Open ezrast opened 5 years ago
I think the ==
here has infinite recursion - to compare Base you have to compare Base first (in a foo field).
This doesn't answers why operator isn't used but can be linked.
Also, section Three
can be replaced with Base.new == Base.new
(equality is used inside Hash implementation)
Another option here would be to include Comparable(Base)
and implement a <=>
method.
The self
restriction here causes problems, coupled with the macro method. It's a known issue (to me), I don't know why it happens. It happened in Struct
too. You can use this as a workaround.
Might be related to #6760
Reduced:
class A
def bar(other)
1
end
end
class B
def bar(other : self)
2
end
def bar(other)
3
end
end
class C < B
def foo
A.new.as(A | C)
end
def bar(other : self)
{% @type %}
foo.bar other.foo
4
end
end
class D < C
end
def test
{% D.methods %} # => []
D.new.bar(D.new) # => 4
{% D.methods %} # => [def bar(other : self); 2; end, def bar(other); 3; end]
D.new.bar(D.new) # => 2
end
test
Non-macro defs should never have to be copied to subtypes.
Crystal is using
Reference#==
instead ofDerived#==
in the last line of the following code:Commenting out any of the sections marked One, Two, Three will cause the override for == to be used, which I believe is the correct behavior.
carc.in link: https://carc.in/#/r/5lvi