diff options
author | Paul Phillips <paulp@improving.org> | 2009-07-09 18:06:38 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-07-09 18:06:38 +0000 |
commit | 79dc3b49f0ed25fcc7cb33fc8fe1c13a6fdc21b3 (patch) | |
tree | 82b7ad10b7c86db1ad1221cdfd4519ed664ada08 /test/files/run | |
parent | ccfb3b9c1697379335992b085368557297c72e2d (diff) | |
download | scala-79dc3b49f0ed25fcc7cb33fc8fe1c13a6fdc21b3.tar.gz scala-79dc3b49f0ed25fcc7cb33fc8fe1c13a6fdc21b3.tar.bz2 scala-79dc3b49f0ed25fcc7cb33fc8fe1c13a6fdc21b3.zip |
Implementation and test cases for canEqual meth...
Implementation and test cases for canEqual method in case classes. Now
the autogenerated equality method inquires with the argument as to
whether other.canEqual(this) before returning true.
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/canEqualCaseClasses.scala | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/test/files/run/canEqualCaseClasses.scala b/test/files/run/canEqualCaseClasses.scala new file mode 100644 index 0000000000..b1c3eb91de --- /dev/null +++ b/test/files/run/canEqualCaseClasses.scala @@ -0,0 +1,23 @@ +object Test { + case class Foo(x: Int) + case class Bar(y: Int) extends Foo(y) + case class Nutty() { + override def canEqual(other: Any) = true + } + + def assertEqual(x: AnyRef, y: AnyRef) = + assert((x == y) && (y == x) && (x.hashCode == y.hashCode)) + + def assertUnequal(x: AnyRef, y: AnyRef) = + assert((x != y) && (y != x)) + + def main(args: Array[String]): Unit = { + assertEqual(Foo(5), Foo(5)) + assertEqual(Bar(5), Bar(5)) + assertUnequal(Foo(5), Bar(5)) + + // in case there's an overriding implementation + assert(Nutty() canEqual (new AnyRef)) + assert(!(Foo(5) canEqual (new AnyRef))) + } +} |