File tree 1 file changed +61
-0
lines changed 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ import dotty .{Eq ,EqClass }
2
+
3
+ object equality {
4
+
5
+ case class Str (str : String ) extends EqClass [_]
6
+
7
+ case class Num (x : Int ) extends EqClass [Num ]
8
+
9
+ case class Other (x : Int )
10
+
11
+ trait Option [+ T ] extends EqClass [_]
12
+ case class Some [+ T ](x : T ) extends Option [T ]
13
+ case object None extends Option [Nothing ]
14
+
15
+ implicit def eqStr : Eq [Str ] = Eq
16
+ implicit def eqOption [T : Eq ]: Eq [Option [T ]] = Eq
17
+
18
+ def main (args : Array [String ]): Unit = {
19
+ val x = Str (" abc" )
20
+ x == x
21
+
22
+ val n = Num (2 )
23
+ val m = Num (3 )
24
+ n == m
25
+
26
+ Other (1 ) == Other (2 )
27
+
28
+ Some (x) == None
29
+ Some (x) == Some (Str (" " ))
30
+ val z : Option [Str ] = Some (Str (" abc" ))
31
+ z == Some (x)
32
+ z == None
33
+ Some (x) == z
34
+ None == z
35
+
36
+ Other (3 ) == null
37
+ Str (" x" ) == null
38
+ null == Other (3 )
39
+ null == Str (" x" )
40
+ null == null
41
+
42
+ def ddistinct [T : Eq ](xs : List [T ]): List [T ] = xs match {
43
+ case Nil => Nil
44
+ case x :: xs => x :: xs.filterNot(x == _)
45
+ }
46
+
47
+ ddistinct(List (z, z, z))
48
+
49
+ x == n // error
50
+ n == x // error
51
+ x == Other (1 ) // error
52
+ Other (2 ) == x // error
53
+ z == Some (n) // error
54
+ z == n // error
55
+ Some (n) == z // error
56
+ n == z // error
57
+ Other (1 ) == z // error
58
+ z == Other (1 ) // error
59
+ ddistinct(List (z, n)) // error
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments