Created
March 3, 2012 23:31
-
-
Save ToxicFrog/1968890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo(_name: String) { | |
def name = _name | |
def equals(other: Foo) = { | |
println(this, other, name, other.name, name equals other.name) | |
_name equals other.name | |
} | |
} | |
val (one,two,three) = (new Foo("one"), new Foo("two"), new Foo("three")) | |
val s = Seq[Foo](one, two, three) | |
println(s contains one) | |
println(s contains new Foo("one")) | |
println(new Foo("one") == one) | |
println(new Foo("one") equals one) | |
// expected output: true true true true; actual output: true false false true; Foo.equals is called once | |
// EXTRA BONUS CODE: shows that == is not calling equals | |
class Bar { | |
def equals(x: Bar) = { println("bar equals bar"); true } | |
} | |
val b = new Bar | |
println(new Bar == new Bar) | |
println(new Bar equals new Bar) | |
// expected output: true true; actual output: false true; Bar.equals is called once |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment