diff options
author | Martin Odersky <odersky@gmail.com> | 2016-05-04 13:12:26 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2016-05-23 16:11:45 +0200 |
commit | 4936627460ba23c743f43ac82c024daf505aa74f (patch) | |
tree | a8412ca572f126b5e552b86b16dc217db28bb848 /tests/neg | |
parent | 89540268e6c49fb92b9ca61249e46bb59981bf5a (diff) | |
download | dotty-4936627460ba23c743f43ac82c024daf505aa74f.tar.gz dotty-4936627460ba23c743f43ac82c024daf505aa74f.tar.bz2 dotty-4936627460ba23c743f43ac82c024daf505aa74f.zip |
Add new strawman for multiversal equality
Diffstat (limited to 'tests/neg')
-rw-r--r-- | tests/neg/EqualityStrawman2.scala | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/neg/EqualityStrawman2.scala b/tests/neg/EqualityStrawman2.scala new file mode 100644 index 000000000..f71c7d3d7 --- /dev/null +++ b/tests/neg/EqualityStrawman2.scala @@ -0,0 +1,96 @@ +object equality { + + trait Eq[-T, -U] + object Eq extends Eq[Any, Any] + + implicit class EqualsDeco[T](val x: T) extends AnyVal { + def ===[U] (y: U)(implicit ce: Eq[T, U]) = x.equals(y) + } + + type EqEq[T] = Eq[T, T] + + trait EqClass + + implicit def eqAny: Eq[Any, Any] = Eq + + implicit def mixedEq1 : Eq[Any, EqClass] = Eq + implicit def mixedEq1alt: Eq[Any, EqClass] = Eq + implicit def mixedEq2 : Eq[EqClass, Any] = Eq + implicit def mixedEq2alt: Eq[EqClass, Any] = Eq + +// implicit def eqEq[U, T <: U with EqClass[U]]: Eq[T, T] = Eq + + case class Str(str: String) extends EqClass + case class Num(x: Int) extends EqClass + + case class Other(x: Int) + + trait Option[+T] extends EqClass + case class Some[+T](x: T) extends Option[T] + case object None extends Option[Nothing] + + implicit def eqStr: Eq[Str, Str] = Eq + implicit def eqNum: Eq[Num, Num] = Eq + implicit def eqOption[T, U](implicit ce: Eq[T, U]): Eq[Option[T], Option[U]] = Eq + + def main(args: Array[String]): Unit = { + val x = Str("abc") + (Some(x): Option[Str]) === (None: Option[Str]) + + x === x + + val n = Num(2) + val m = Num(3) + n === m + + Other(1) === Other(2) + + (Some(x) === None)//(eqOption) + (Some(x) === Some(Str("")))(eqOption) + val z: Option[Str] = Some(Str("abc")) + z === Some(x) + z === None + Some(x) === z + None === z +/* + Other(3) === null + Str("x") === null + null === Other(3) + null === Str("x") + null === null*/ + + class Fruit extends EqClass + class Apple extends Fruit + class Pear extends Fruit + implicit def eqFruit: Eq[Fruit, Fruit] = Eq + + Some(new Apple) === Some(new Pear) + + + def ddistinct[T: EqEq](xs: List[T]): List[T] = xs match { + case Nil => Nil + case x :: xs => x :: xs.filterNot(x === _) + } + + ddistinct(List(z, z, z)) + + n match { + case None => + } + + Some(new Apple) === Some(Str("xx")) // error + x === n // error + n === x // error + x === Other(1) // error + Other(2) === x // error + z === Some(n) // error + z === n // error + Some(n) === z // error + n === z // error + Other(1) === z // error + z === Other(1) // error + ddistinct(List(z, n)) // error + + + } +} |