File tree Expand file tree Collapse file tree 2 files changed +161
-0
lines changed Expand file tree Collapse file tree 2 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ object equality {
3
+
4
+ case class Str (str : String ) extends EqClass
5
+
6
+ case class Num (x : Int ) extends EqClassOf [Num ]
7
+
8
+ case class Other (x : Int )
9
+
10
+ trait Option [+ T ] extends EqClass
11
+ case class Some [+ T ](x : T ) extends Option [T ]
12
+ case object None extends Option [Nothing ]
13
+
14
+ implicit def eqStr : Eq [Str ] = Eq
15
+ implicit def eqOption [T : Eq ]: Eq [Option [T ]] = Eq
16
+
17
+ class PreName extends EqClass
18
+ implicit def eqPreName : Eq [PreName ] = Eq
19
+
20
+ implicit def fromString (str : String ): PreName = ???
21
+ val name = " abc"
22
+ name == " def"
23
+
24
+
25
+
26
+ def main (args : Array [String ]): Unit = {
27
+ val x = Str (" abc" )
28
+ x == x
29
+
30
+ val n = Num (2 )
31
+ val m = Num (3 )
32
+ n == m
33
+
34
+ Other (1 ) == Other (2 )
35
+
36
+ Some (x) == None
37
+ Some (x) == Some (Str (" " ))
38
+ val z : Option [Str ] = Some (Str (" abc" ))
39
+ z == Some (x)
40
+ z == None
41
+ Some (x) == z
42
+ None == z
43
+
44
+ Other (3 ) == null
45
+ Str (" x" ) == null
46
+ null == Other (3 )
47
+ null == Str (" x" )
48
+ null == null
49
+
50
+ class Fruit extends EqClass
51
+
52
+ implicit def eqFruit : Eq [Fruit ] = Eq
53
+ class Apple extends Fruit
54
+ class Pear extends Fruit
55
+ val a = new Apple
56
+ val p = new Pear
57
+ val f : Fruit = a
58
+ a == p
59
+ p == a
60
+ f == p
61
+ p == f
62
+ Some (new Apple ) == Some (new Pear )
63
+
64
+ def ddistinct [T : Eq ](xs : List [T ]): List [T ] = xs match {
65
+ case Nil => Nil
66
+ case x :: xs => x :: xs.filterNot(x == _)
67
+ }
68
+
69
+ ddistinct(List (z, z, z))
70
+
71
+ x == Other (1 ) // error
72
+ Other (2 ) == x // error
73
+ Other (1 ) == z // error
74
+ z == Other (1 ) // error
75
+
76
+ }
77
+ }
Original file line number Diff line number Diff line change
1
+
2
+ object equality {
3
+
4
+ case class Str (str : String ) extends EqClass
5
+
6
+ case class Num (x : Int ) extends EqClassOf [Num ]
7
+
8
+ case class Other (x : Int )
9
+
10
+ trait Option [+ T ] extends EqClass
11
+ case class Some [+ T ](x : T ) extends Option [T ]
12
+ case object None extends Option [Nothing ]
13
+
14
+ implicit def eqStr : Eq [Str ] = Eq
15
+ implicit def eqOption [T : Eq ]: Eq [Option [T ]] = Eq
16
+
17
+ class PreName extends EqClass
18
+ implicit def eqPreName : Eq [PreName ] = Eq
19
+
20
+ implicit def fromString (str : String ): PreName = ???
21
+ val name = " abc"
22
+ name == " def"
23
+
24
+
25
+
26
+ def main (args : Array [String ]): Unit = {
27
+ val x = Str (" abc" )
28
+ x == x
29
+
30
+ val n = Num (2 )
31
+ val m = Num (3 )
32
+ n == m
33
+
34
+ Other (1 ) == Other (2 )
35
+
36
+ Some (x) == None
37
+ Some (x) == Some (Str (" " ))
38
+ val z : Option [Str ] = Some (Str (" abc" ))
39
+ z == Some (x)
40
+ z == None
41
+ Some (x) == z
42
+ None == z
43
+
44
+ Other (3 ) == null
45
+ Str (" x" ) == null
46
+ null == Other (3 )
47
+ null == Str (" x" )
48
+ null == null
49
+
50
+ class Fruit extends EqClass
51
+
52
+ implicit def eqFruit : Eq [Fruit ] = Eq
53
+ class Apple extends Fruit
54
+ class Pear extends Fruit
55
+ val a = new Apple
56
+ val p = new Pear
57
+ val f : Fruit = a
58
+ a == p
59
+ p == a
60
+ f == p
61
+ p == f
62
+ Some (new Apple ) == Some (new Pear )
63
+
64
+ def ddistinct [T : Eq ](xs : List [T ]): List [T ] = xs match {
65
+ case Nil => Nil
66
+ case x :: xs => x :: xs.filterNot(x == _)
67
+ }
68
+
69
+ ddistinct(List (z, z, z))
70
+
71
+ n match {
72
+ case None => // error
73
+ }
74
+
75
+ Some (new Apple ) == Some (Str (" xx" )) // error
76
+ x == n // error
77
+ n == x // error
78
+ z == Some (n) // error
79
+ z == n // error
80
+ Some (n) == z // error
81
+ n == z // error
82
+ ddistinct(List (z, n)) // error
83
+ }
84
+ }
You can’t perform that action at this time.
0 commit comments