-
-
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
[RFC] Make some cases of Array#==(Tuple) and Tuple#==(Array) return true #529
Comments
I thought it would work that way already. It seems common sense. At the very least, I think Array#=== should work like that. |
I think we can add these comparisons. Ideally comparing Array, StaticArray, Slice and Tuple between them should work: the comparison would be by length and contents. But I'd like to know waj's opinion, let's wait until he comes back :-) |
any update on this one? :) @waj |
@waj please change the draft label to accepted or close the issue when you had time to form your opinion on this. |
Guys is there a roadmap about those classes Slice, StaticArray, etc.? Such as this that helped in 2008 to understand Ruby classes inheritance, but maybe related more to the implementation to teach how can I use low level Crystal features for program performance. |
+1
|
There hasn't been any feedback from @waj for two and a half years. So maybe this needs to go on without his opinion. There has not been any argument why this shouldn't be implemented. It makes sense to be able to compare collections of different types directly. And I'd add |
Overriding It seems to me we could define [0, 2, 3].same_elements?({0, 2, 3}) # => true The def same_elements?(other : Indexable)
size == other.size && (0...size).all? do |i|
unsafe_fetch(i) == other.unsafe_fetch(i)
end
end |
Maybe even |
So, we have |
Not possible at this point, because |
An overload |
Actually: module Indexable(T)
def equals?(other : Indexable)
equals?(other) { |x, y| x == y }
end
end |
Java's |
It turns out [0, 2, 3] == Deque{0, 2, 3} # => false
[0, 2, 3].hash == Deque{0, 2, 3}.hash # => true That shouldn't be. I'm now convinced that implementing a generic Btw. for Tuple both are false because Tuple overrides |
I don't see how |
|
But hash equality doesn't imply equality. I can't see how a deque is the same thing (equal) as an array. Or any other collection for that matter. We can probably add a same_elements? method. But it's not equality. |
Yeah, it doesn't. But when we already use a generic hash implementation for Let's not talk about equality but comparability: Int32, UInt8 and BigInt all represent the same concept - a number - expressed in different data types with different specific properties. But the values are nevertheless comparable. Comparability is expressed in the The equality operator Maybe we should have separate methods for both concepts. But I actually don't see much use cases for the stricter definition, which could be expressed as |
I think what you say makes sense. Plus it's probably harmless to allow such comparisons, and it can actually be useful. |
I have made up my mind, I am definitely against making The most I'd allow is One of your arguments in the PR:
If you keep moving in that direction, you can end up with One particular situation is, imagine a type that happens to be indexable but also has other fields. If you add that field to your class, and you want it to be equal only to other instances that have the same field, you're out of options, other indexables will forcibly compare equal to yours. You also have your hands tied in terms of the In fact, it doesn't need to include other fields. Check out this VideoFrame class. Should a video frame ever be equal to an array? Should a Matrix ever be equal to a deque? |
Good points. I actually don't know what's the use case behind all of this. |
No that's not the direction I'm pointing.
Yes, that would be a limitation. But is there any practical relevance to that? Or in other words: A compound type that is decisively more than a container of items probably shouldn't include The examples you mentioned seem to fit perfectly fine with |
This could allow for some nice and efficient comparisons, for example
bytes[0..2] == {0x00, 0x02, 0x03}
instead ofbytes[0..2] == [0x00, 0x02, 0x03]
, which would save one array allocation.The text was updated successfully, but these errors were encountered: