-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Commutative equality #13409
Comments
Additionally, there would also be a technical option to ensure commutativity of But this would likely still be brittle. Specific implementations could still revert to the default implementation for some cases. Or even if there are specific implementations for both directions, does not necessarily mean they are required to agree 😆 |
Any comparison operator can be concerned by such inconsistencies, like |
Good point. However comparison operates are typically implemented via the The case equality operator (1..2) === 1 # => true
1 === (1..2) # => false So I'd argue it's not actually an equality, it's more pattern matching. But that's a different story. There's also the pattern match operator |
For standard library specs we could consider using something like this in as many places as possible: crystal/spec/std/big/big_rational_spec.cr Lines 8 to 23 in 98c091e
|
Like I said in the previous post, I don't think there is much need for this. The only implementations of these comparison operators in stdlib are in All other types implement the derived operators through |
While we're talking about strictness of the
eq
expectation in #13389, I'd like to push some other though that has bugged me for a while.Equality is usually expected to be commutative (i.e. from
a == b
followsb == a
). That's a practical assumption, but not necessarily the case. Ifa
andb
have different types (sayA
andB
), it might be that only one leg,A#==(B)
orB#==(A)
is implemented and the inverse would fall back to the default implementation which always returnsfalse
(ref #10277).In stdlib there are a couple of cases where this applies. They're related to types that imitate a wrapped type, namely
YAML::Any
andLog::Metadata::Value
.I think that these types that implement custom equality methods should do the same on the types they define equality with.
Having specs ensure equality in both directions should go a long way to provide sanity on this topic.
If type strictness gets implemented as proposed in #13389, this will also cover specs for all such cases because
a eq b
requires both operands to have the same type. Otherwise, it would be good foreq
to test reverse equality.Patch
Failing examples with this patch:
The text was updated successfully, but these errors were encountered: