Open dzhang314 opened 1 year ago
Oof, that is unfortunate. I'll see if I can tweak Base to fix this..
Or alternatively, we have to implement this method ourselves. Maybe a vectorized ==
was a mistake. You can't do stuff like
julia> [Vec(1, 2), Vec(3, 4)] == [Vec(5, 6), Vec(7, 8)]
ERROR: TypeError: non-boolean (Vec{2, Bool}) used in boolean context
Maybe we should just make a ==ᵥ
or a .==
that does the vectorized version, and have regular ==
compare the whole Vec
?
Probably yes.
But this should apply to eg Symbolics as well so maybe the Base behavior can be improved.
We can overload tuple comparisons for SIMD vector elements. I don't think this would be type piracy because we own the SIMD types. This would be a simple approach and would do what people expect. We are explicitly circumventing an "unfortunate choice" in tuple comparisons.
SIMD.jl does owns Vec
, but it does not own Tuple{Vec}
: https://docs.julialang.org/en/v1/manual/style-guide/#Don't-overload-methods-of-base-container-types
Given the rationale there – "This would provide custom showing of vectors with a specific new element type. While tempting, this should be avoided. The trouble is that users will expect a well-known type like Vector() to behave in a certain way, and overly customizing its behavior can make it harder to work with." – one could argue that overloading tuple comparisons is fine here because we're correcting an unexpected behaviour coming from an unfortunate choice made in Base.
Of course, correcting this in Base would be better.
Sure. Sometimes piracy is useful and needed (at least as a stopgap). I'm just pointing out that it is piracy.
Yes, thank you. Indeed I did not realize that.
I ran into the following issue while working with
NTuple
s ofVec
s:The tuples
(Vec(1, 2), Vec(3, 4))
and(Vec(5, 6), Vec(7, 8))
are absolutely not the same, but when compared with==
, Julia thinks they are! This comes from an unfortunate quirk of the wayBase._eq
is defined in tuple.jl, where any comparison result that is not explicitlyfalse
is treated astrue
.