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

Better handling of Hash with default block but of the wrong type #15057

Open
Blacksmoke16 opened this issue Oct 3, 2024 · 4 comments
Open

Better handling of Hash with default block but of the wrong type #15057

Blacksmoke16 opened this issue Oct 3, 2024 · 4 comments
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. status:discussion topic:stdlib:collection

Comments

@Blacksmoke16
Copy link
Member

Discussion

  • What aspect of the language would you like to see improved?

I'd like to see the error message related to missing hash keys improved to make some edge cases more clear.

  • What are the reasons?

I kept getting a Missing hash key exception and was very confused because the exception included the key and because of the default block, should not be possible. It ultimately was because of

if (block = @block) && key.is_a?(K)
where my key wasn't actually the type I thought it was raising the error vs calling the block.

  • Include practical examples to illustrate your points.
record SomeThing, message : String do
  def inspect(io)
    @message.inspect io
  end
end

hits = Hash(String, Hash(Int32, Int32)).new { |hash, key| hash[key] = Hash(Int32, Int32).new(0) }

hits["foo"][0] = 2 # Fine

thing = SomeThing.new "bar"

# Very confusing because it _looks_ like a string.
hits[thing][0] = 3 # Unhandled exception: Missing hash key: "bar" (KeyError)
  • Optionally add one (or more) proposals to improve the current situation.

Solutions I could think of are either to maybe have a dedicated error path when there is a block but its type doesn't match, or include the type of the key in the existing error message.

@Blacksmoke16 Blacksmoke16 added good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. status:discussion topic:stdlib:collection labels Oct 3, 2024
@devnote-dev
Copy link
Contributor

I do wonder why Hash#[] and Hash#[]? aren't strictly typed, that solves this with a compile time error. I guess it's too late to change it now...

@Blacksmoke16
Copy link
Member Author

@devnote-dev See #8893.

@straight-shoota
Copy link
Member

I suppose it should be trivial to enhance the error message when @block && !key.is_a?(K) to include something like (key is not #{ K }).

@rajatkriplani

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This is an issue suited for newcomers to become aquianted with working on the codebase. status:discussion topic:stdlib:collection
Projects
None yet
Development

No branches or pull requests

4 participants