@@ -19,18 +19,104 @@ object Test extends App {
19
19
assert(h(1 )(1 )(2 ) == 2 )
20
20
assert(h(1 )(1 )(" two" ) == 3 )
21
21
22
- implied Foo {
23
- def (x : Int ) |+| (y : Int ) = x + y
24
- def (x : Int ) |+| (y : String ) = x + y.length
22
+ // Test with extension methods in implied object
23
+ object test1 {
25
24
26
- def (xs : List [T ]) +++ [T ] (ys : List [T ]): List [T ] = xs ++ ys ++ ys
27
- def (xs : List [T ]) +++ [T ] (ys : Iterator [T ]): List [T ] = xs ++ ys ++ ys
25
+ implied Foo {
26
+ def (x : Int ) |+| (y : Int ) = x + y
27
+ def (x : Int ) |+| (y : String ) = x + y.length
28
+
29
+ def (xs : List [T ]) +++ [T ] (ys : List [T ]): List [T ] = xs ++ ys ++ ys
30
+ def (xs : List [T ]) +++ [T ] (ys : Iterator [T ]): List [T ] = xs ++ ys ++ ys
31
+ }
32
+
33
+ assert((1 |+| 2 ) == 3 )
34
+ assert((1 |+| " 2" ) == 2 )
35
+
36
+ val xs = List (1 , 2 )
37
+ assert((xs +++ xs).length == 6 )
38
+ assert((xs +++ xs.iterator).length == 4 , xs +++ xs.iterator)
39
+ }
40
+ test1
41
+
42
+ // Test with imported extension methods
43
+ object test2 {
44
+ import test1 .Foo ._
45
+
46
+ assert((1 |+| 2 ) == 3 )
47
+ assert((1 |+| " 2" ) == 2 )
48
+
49
+ val xs = List (1 , 2 )
50
+ assert((xs +++ xs).length == 6 )
51
+ assert((xs +++ xs.iterator).length == 4 , xs +++ xs.iterator)
28
52
}
53
+ test2
54
+
55
+ // Test with implied extension methods coming from base class
56
+ object test3 {
57
+ class Foo {
58
+ def (x : Int ) |+| (y : Int ) = x + y
59
+ def (x : Int ) |+| (y : String ) = x + y.length
60
+
61
+ def (xs : List [T ]) +++ [T ] (ys : List [T ]): List [T ] = xs ++ ys ++ ys
62
+ def (xs : List [T ]) +++ [T ] (ys : Iterator [T ]): List [T ] = xs ++ ys ++ ys
63
+ }
64
+ implied Bar for Foo
65
+
66
+ assert((1 |+| 2 ) == 3 )
67
+ assert((1 |+| " 2" ) == 2 )
68
+
69
+ val xs = List (1 , 2 )
70
+ assert((xs +++ xs).length == 6 )
71
+ assert((xs +++ xs.iterator).length == 4 , xs +++ xs.iterator)
72
+ }
73
+ test3
74
+
75
+ // Test with implied extension methods coming from implied alias
76
+ object test4 {
77
+ implied for test3.Foo = test3.Bar
78
+
79
+ assert((1 |+| 2 ) == 3 )
80
+ assert((1 |+| " 2" ) == 2 )
29
81
30
- assert((1 |+| 2 ) == 3 )
31
- assert((1 |+| " 2" ) == 2 )
82
+ val xs = List (1 , 2 )
83
+ assert((xs +++ xs).length == 6 )
84
+ assert((xs +++ xs.iterator).length == 4 , xs +++ xs.iterator)
85
+ }
86
+ test4
87
+
88
+ class C {
89
+ def xx (x : Any ) = 2
90
+ }
91
+ def (c : C ) xx (x : Int ) = 1
92
+
93
+ val c = new C
94
+ assert(c.xx(1 ) == 2 ) // member method takes precedence
95
+
96
+ object D {
97
+ def (x : Int ) yy (y : Int ) = x + y
98
+ }
99
+
100
+ implied {
101
+ def (x : Int ) yy (y : Int ) = x - y
102
+ }
103
+
104
+ import D ._
105
+ assert((1 yy 2 ) == 3 ) // imported extension method takes precedence
106
+
107
+ trait Rectangle {
108
+ def a : Long
109
+ def b : Long
110
+ }
111
+
112
+ case class GenericRectangle (a : Long , b : Long ) extends Rectangle
113
+ case class Square (a : Long ) extends Rectangle {
114
+ def b : Long = a
115
+ }
32
116
33
- val xs = List (1 , 2 )
34
- assert((xs +++ xs).length == 6 )
35
- assert((xs +++ xs.iterator).length == 4 , xs +++ xs.iterator)
117
+ def (rectangle : Rectangle ) area : Long = 0
118
+ def (square : Square ) area : Long = square.a * square.a
119
+ val rectangles = List (GenericRectangle (2 , 3 ), Square (5 ))
120
+ val areas = rectangles.map(_.area)
121
+ assert(areas.sum == 0 )
36
122
}
0 commit comments