Skip to content
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

Type interference breaks equality operator #230

Closed
jhass opened this issue Oct 13, 2014 · 1 comment
Closed

Type interference breaks equality operator #230

jhass opened this issue Oct 13, 2014 · 1 comment

Comments

@jhass
Copy link
Member

jhass commented Oct 13, 2014

Okay, not a good title, but this one is rather weird :P

class EqualMatcher
  def initialize(@other)
  end

  def matches(obj)
    obj == @other
  end
end

class Expectation
  def initialize(@target)
  end

  def to(matcher)
    pp self
    pp @target unless matcher.matches(@target)
  end
end

Expectation.new([1]).to EqualMatcher.new([1])
Expectation.new([1, nil]).to EqualMatcher.new([1, nil])
Expectation.new([1] == [1]).to EqualMatcher.new(true)

Output:

self = #<Expectation(Array(Int32)):0x13A9FA0 @target=[1]>
self = #<Expectation(Array((Int32 | Nil))):0x13A9F20 @target=[1, nil]>
self = #<Expectation(Bool):0x13ABFB0 @target=false>
@target = false

Changing to EqualMatcher to

class EqualMatcher(T)
  def initialize(@other : T)
  end

  def matches(obj)
    obj == @other
  end
end

fixes the result (no @target = false in the output). My guess is a missing error case / exception.

@asterite
Copy link
Member

This one is a serious one.

A smaller code, but having the same issue, is this one:

class Base
  def some(other : self)
    false
  end

  def some(other)
    false
  end
end

class Foo(T) < Base
  def some(other : Foo)
    true
  end
end

a = Foo(Int32).new
b = Foo(Int32).new || Foo(Int32 | Nil).new || true
a.some(b)

c = Foo(Int32).new
z = c.some(c)
puts z

Outputs "false" instead of "true".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants