blob: 4a77a2d259689732528a11abf5323ea7a1bc4b70 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
object equality {
case class Str(str: String)
case class Num(x: Int)
case class Other(x: Int)
trait Option[+T]
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 e: Eq[T, U]): Eq[Option[T], Option[U]] = Eq
def main(args: Array[String]): Unit = {
Some(Other(3)) == None
val x = Str("abc")
x == x
val n = Num(2)
val m = Num(3)
n == m
Other(1) == Other(2)
Some(x) == None
Some(x) == Some(Str(""))
val z: Option[Str] = Some(Str("abc"))
z == Some(x)
z == None
Some(x) == z
None == z
None == Some(Other(3))
Other(3) == null
Str("x") == null
null == Other(3)
null == Str("x")
null == null
class Fruit
implicit def eqFruit: Eq[Fruit, Fruit] = Eq
class Apple extends Fruit
class Pear extends Fruit
val a = new Apple
val p = new Pear
val f: Fruit = a
a == p
p == a
f == p
p == f
Some(new Apple) == Some(new Pear)
n match {
case None => // error
}
x == Other(1) // error
Other(2) == x // error
Other(1) == z // error
z == Other(1) // error
n == None // error
Some(new Apple) == Some(Str("xx")) // error
x == n // error
n == x // error
z == Some(n) // error
z == n // error
Some(n) == z // error
Some(n) == Some(Other(3)) // error
Some(Other(3)) == Some(n) // error
n == z // error
}
}
|