You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Comparable overrides only 5 out of the 6 relational operators. The sole exception, #!=, will return true if the two operands are unordered, because it is effectively !(self == other):
x =Float64::NAN
y = {x}
# `Float64#!=` called
x <=> x # => nil
x != x # => false# `Object#!=` called
y <=> y # => nil
y != y # => true
The results become consistent if the following is defined:
moduleComparable(T)
def!=(other : T) : Bool
cmp =self<=> other
cmp ? cmp !=0 : falseendend# `Float64#!=` overrides `Comparable#!=`
x != x # => false# `Comparable#!=` called
y != y # => false
Is there any reason this is not done in the standard library?
The text was updated successfully, but these errors were encountered:
x != x #=> false is actually very surprising. The implementation of Float#!= seems to be missing a special case for NaN (which Float#== apparently covers).
I'd consider if there's even a good reason to override Object#!= at all. The default implementation !(self == other) is definitely correct.
The primary problem is that Float#!= is an override with wrong semantics.
Comparable
overrides only 5 out of the 6 relational operators. The sole exception,#!=
, will return true if the two operands are unordered, because it is effectively!(self == other)
:The results become consistent if the following is defined:
Is there any reason this is not done in the standard library?
The text was updated successfully, but these errors were encountered: