Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Kernel#Hash #1436

Conversation

sampersand
Copy link
Contributor

This updates Kernel#Hash's signature.

This is probably the second-simplest of the kernel conversion methods, after Kernel#String:

  • If the argument is nil or an empty array, an empty hash is returned.
  • Otherwise, .to_hash is called on the object.

I was debating on whether I should make a separate (nil | []) -> {} case, but I decided against it. (Using [] / {} literals in practice is pretty difficult, as it's a Hash[bot, bot].)

@sampersand sampersand force-pushed the swesterman/23-08-18/update-Kernel#Hash branch from 56f9c72 to 7cc53df Compare August 18, 2023 21:39
@@ -488,7 +488,7 @@ module Kernel : BasicObject
# Hash(nil) # => {}
# Hash([]) # => {}
#
def self?.Hash: [K, V] (Object x) -> ::Hash[K, V]
def self?.Hash: [K, V] (nil | [] | _ToHash[K, V]) -> Hash[K, V]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating on whether I should make a separate (nil | []) -> {} case, but I decided against it.

No need to, at all, since nil and empty arrays already have #to_h, let their own RBSs do the [K, V] magic. (I’ll check RBS for NilClass#to_h)

Suggested change
def self?.Hash: [K, V] (nil | [] | _ToHash[K, V]) -> Hash[K, V]
def self?.Hash: [K, V] (_ToHash[K, V]) -> Hash[K, V]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NilClass#to_h gives Hash[untyped, untyped] 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the exact same @ParadoxV5_ToHash requires to_hash, not to_h. So the nil | [] case is distinct.

@sampersand
Copy link
Contributor Author

Consolidated in #1445

@sampersand sampersand closed this Aug 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants