Skip to content

Type interference breaks equality operator #230

Closed
@jhass

Description

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions