blob: 4434aa1601476589745b1274f5c9179a43fed59c (
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
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
implicit def eqString: Eq[String, String] = Eq
implicit def eqInt: Eq[Int, Int] = Eq
implicit def eqNumber: Eq[Number, Number] = Eq
implicit def eqIntNumber: Eq[Int, Number] = Eq
implicit def eqNumberInt: Eq[Number, Int] = 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
val so: Object = "abc"
so == "abc"
"abc" == so
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)
val i = 3
val bi = BigInt(i)
i == i
bi == bi
i == bi
bi == i
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
"abc" == 1 // error
1 == "abc" // error
"abc" == bi // error
bi == "abc" // error
}
}
|