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

Fix Range#== to ignore generic type arguments #10309

Merged
merged 1 commit into from
Feb 12, 2021

Conversation

straight-shoota
Copy link
Member

(1..1) == (1.0..1.0) currently returns false. The equality operator Struct#== includes a type restriction to self but the types are different because of generic type arguments: Range(Int32, Int32) and Range(Float64, Float64).

The same problem appears with nilable generic type arguments like comparing Range(Int32?, Int32?) with Range(Int32, Int32) always fails, even if both have identical begin and end values.

This patch overrides the equality operator as Range#==(other : Range) which defines quality based on actual values, ignoring generic type arguments.

A more comprehensive fix would be to change the type restriction in Struct#== from self to the generic class type. I'll look into that, too. But this is a minimal fix for now and IMO worth merging as is.

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection labels Jan 27, 2021
src/range.cr Show resolved Hide resolved
@bcardiff bcardiff merged commit 6310431 into crystal-lang:master Feb 12, 2021
@straight-shoota straight-shoota deleted the fix/range-eq branch February 13, 2021 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:collection
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants