Skip to content

Enhance error message for Hash#[] when key is wrong type for default block#16442

Merged
straight-shoota merged 4 commits intocrystal-lang:masterfrom
Blacksmoke16:hash-default-block-wrong-type
Dec 2, 2025
Merged

Enhance error message for Hash#[] when key is wrong type for default block#16442
straight-shoota merged 4 commits intocrystal-lang:masterfrom
Blacksmoke16:hash-default-block-wrong-type

Conversation

@Blacksmoke16
Copy link
Member

@Blacksmoke16 Blacksmoke16 commented Nov 27, 2025

Fixes #15057

@straight-shoota
Copy link
Member

Maybe the message should not be Missing hash key but something like Invalid key type.
When there's a block, it would be technically possible to return a value for any key, as long as it has the right type. So the problem is that first we didn't find the key in the hash, and second we couldn't use it for the default block. It might make sense for the latter to take precedence...

@Blacksmoke16
Copy link
Member Author

How about "Invalid key type: expected #{K}, got #{key.class}"

@straight-shoota straight-shoota added this to the 1.19.0 milestone Nov 27, 2025
@Sija
Copy link
Contributor

Sija commented Nov 28, 2025

Isn't this solution going to create issues re: lack of symmetry of the error messages? Why is the type checking being done only when the block is being given and not in other cases? The same method will return different errors depending on the internal state, violating the principle of the least surprise.

@ysbaddaden
Copy link
Collaborator

Couldn't the error message could outline the key type mismatch regardless of the block?

Another hint in favor to #8893 IMO.

@straight-shoota
Copy link
Member

A maybe surprising property is that type identity is not a requirement for hash lookup.
When looking up in the hash, it's not even necessary that the lookup key matches the hash's key type. It can match values of different types, as long as they're equal in #== and have an identical #hash.
Matching type is only necessary when calling the default block. So I think it's adequate to have this special error message about incompatible type only when a block is given, because at that point it's no longer about matching to a key in the hash, but calling a proc which expects a specific input type.

@Sija
Copy link
Contributor

Sija commented Nov 28, 2025

@straight-shoota The error message doesn't mention block at all though which makes this confusing.

@straight-shoota straight-shoota merged commit 034a6dc into crystal-lang:master Dec 2, 2025
40 checks passed
@straight-shoota straight-shoota changed the title Enhance error message Hash has a block, but the key is missing and is of the wrong type Enhance error message for Hash#[] when key is wrong type for default block Dec 2, 2025
@Blacksmoke16 Blacksmoke16 deleted the hash-default-block-wrong-type branch December 5, 2025 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants