@@ -29,9 +29,39 @@ object Test {
29
29
val z3 = d2[E = Int ](1 )
30
30
val z4 = d2[V = Int ](" AAA" )
31
31
val z5 = d2[E = Int ][V = String ](1 )
32
+
33
+ // Testing type inference
34
+
35
+ def f [X <: C ](x : X [Int , Int ]): X [String , String ] = ???
36
+ val arg1 : C [Int , Int ] = ???
37
+ val res1 = f(arg1)
38
+ val chk1 : C [String , String ] = res1
39
+
40
+ class C1 [type Elem , type Value ](x : Elem ) extends C [Elem , Value ](x)
41
+ class CC extends C1 [Int , Int ](1 )
42
+ val arg2 : CC = ???
43
+ val res2 = f(arg2)
44
+ val chk2 : C [String , String ] = res2
45
+
46
+ class D1 [type Elem , type Value ](x : Elem ) extends C [Elem , Value ](x)
47
+ class DD extends D1 [Int , Int ](2 )
48
+ val arg3 : CC & DD = ???
49
+ val res3 = f(arg3)
50
+ val chk3 : (C1 & D1 ) { type Elem = String ; type Value = String } = res3
51
+ val arg4 : CC | DD = ???
52
+ val res4 = f(arg4)
53
+ val chk4 : C [String , String ] = ???
54
+
55
+ class CX [type Elem ](x : Elem ) extends C1 [Elem , Int ](x)
56
+ class DX [type Value ]() extends D1 [Int , Value ](2 )
57
+ val arg5 : CX [Int ] & DX [Int ] = ???
58
+ val res5 = f(arg5)
59
+ val chk5 : (C1 & D1 ) { type Elem = String ; type Value = String } = res5
60
+ val chk6 : C1 [String , String ] & D1 [String , String ] = chk5
61
+ val chk7 : (C1 & D1 ) { type Elem = String ; type Value = String } = chk6
32
62
}
33
63
34
- // Adapated from i94-nada
64
+ // Adapted from i94-nada, somewhat non-sensical
35
65
trait Test1 {
36
66
trait Monad [type Elem ] {
37
67
def unit : Elem
@@ -40,7 +70,21 @@ trait Test1 {
40
70
case class Left [A ,B ](unit : A ) extends Either [A ,B ] with Monad [A ]
41
71
case class Right [A ,B ](unit : B ) extends Either [A ,B ] with Monad [B ]
42
72
def flatMap [X ,Y ,M <: Monad ](m : M [Elem = X ], f : X => M [Elem = Y ]): M [Elem = Y ] = f(m.unit)
43
- println(flatMap(Left (1 ), {x : Int => Left (x)}))
73
+ val res = flatMap(Left (1 ), {x : Int => Left (x)})
74
+ val chk : Either [Int , Nothing ] & Monad & Product1 [Int ] = res
75
+ }
76
+
77
+ // Adapted from i94-nada, this time with more sense
78
+ trait Test2 {
79
+ trait Monad [type Elem ] {
80
+ def unit : Elem
81
+ }
82
+ sealed abstract class Either [A ,B ]
83
+ case class Left [type Elem , B ](unit : Elem ) extends Either [Elem ,B ] with Monad [Elem ]
84
+ case class Right [A , type Elem ](unit : Elem ) extends Either [A ,Elem ] with Monad [Elem ]
85
+ def flatMap [X ,Y ,M <: Monad ](m : M [Elem = X ], f : X => M [Elem = Y ]): M [Elem = Y ] = f(m.unit)
86
+ val res = flatMap(Left (1 ), {x : Int => Left (x)})
87
+ val chk : Left [Int , Nothing ] = res
44
88
}
45
89
46
90
0 commit comments