|
1 | 1 | import scala.reflect.Selectable.reflectiveSelectable
|
2 | 2 |
|
3 | 3 | object Test {
|
| 4 | + class C { type S = String; type I } |
| 5 | + class D extends C { type I = Int } |
| 6 | + |
4 | 7 | type Foo = {
|
5 |
| - def foo(x: Int)(y: Int): Int |
6 |
| - def bar(x: Int): Int => Int |
7 |
| - def bat(a1: Int, a2: Int, a3: Int) |
8 |
| - (a4: Int, a5: Int, a6: Int) |
9 |
| - (a7: Int, a8: Int, a9: Int): Int |
10 |
| - def baz(x: Int): Int |
| 8 | + def sel0: Int |
| 9 | + def sel1: Int => Int |
| 10 | + def fun0(x: Int): Int |
| 11 | + |
| 12 | + def fun1(x: Int)(y: Int): Int |
| 13 | + def fun2(x: Int): Int => Int |
| 14 | + def fun3(a1: Int, a2: Int, a3: Int) |
| 15 | + (a4: Int, a5: Int, a6: Int) |
| 16 | + (a7: Int, a8: Int, a9: Int): Int |
| 17 | + |
| 18 | + def fun4(implicit x: Int): Int |
| 19 | + def fun5(x: Int)(implicit y: Int): Int |
| 20 | + |
| 21 | + def fun6(x: C, y: x.S): Int |
| 22 | + def fun7(x: C, y: x.I): Int |
| 23 | + def fun8(y: C): y.S |
| 24 | + def fun9(y: C): y.I |
11 | 25 | }
|
12 | 26 |
|
13 | 27 | class FooI {
|
14 |
| - def foo(x: Int)(y: Int): Int = x + y |
15 |
| - def bar(x: Int): Int => Int = y => x * y |
16 |
| - def bat(a1: Int, a2: Int, a3: Int) |
17 |
| - (a4: Int, a5: Int, a6: Int) |
18 |
| - (a7: Int, a8: Int, a9: Int): Int = -1 |
19 |
| - def baz(x: Int): Int = x |
| 28 | + def sel0: Int = 1 |
| 29 | + def sel1: Int => Int = x => x |
| 30 | + def fun0(x: Int): Int = x |
| 31 | + |
| 32 | + def fun1(x: Int)(y: Int): Int = x + y |
| 33 | + def fun2(x: Int): Int => Int = y => x * y |
| 34 | + def fun3(a1: Int, a2: Int, a3: Int) |
| 35 | + (a4: Int, a5: Int, a6: Int) |
| 36 | + (a7: Int, a8: Int, a9: Int): Int = -1 |
| 37 | + |
| 38 | + def fun4(implicit x: Int): Int = x |
| 39 | + def fun5(x: Int)(implicit y: Int): Int = x + y |
| 40 | + |
| 41 | + def fun6(x: C, y: x.S): Int = 1 |
| 42 | + def fun7(x: C, y: x.I): Int = 2 |
| 43 | + def fun8(y: C): y.S = "Hello" |
| 44 | + def fun9(y: C): y.I = 1.asInstanceOf[y.I] |
| 45 | + } |
| 46 | + |
| 47 | + def basic(x: Foo): Unit ={ |
| 48 | + assert(x.sel0 == 1) |
| 49 | + assert(x.sel1(2) == 2) |
| 50 | + assert(x.fun0(3) == 3) |
| 51 | + |
| 52 | + val f = x.sel1 |
| 53 | + assert(f(3) == 3) |
| 54 | + } |
| 55 | + |
| 56 | + def currying(x: Foo): Unit = { |
| 57 | + assert(x.fun1(1)(2) == 3) |
| 58 | + assert(x.fun2(1)(2) == 2) |
| 59 | + assert(x.fun3(1, 2, 3)(4, 5, 6)(7, 8, 9) == -1) |
20 | 60 | }
|
21 | 61 |
|
22 |
| - def test(x: Foo): Unit = { |
23 |
| - assert(x.foo(1)(2) == 3) |
24 |
| - assert(x.bar(1)(2) == 2) |
25 |
| - assert(x.bat(1, 2, 3)(4, 5, 6)(7, 8, 9) == -1) |
| 62 | + def etaExpansion(x: Foo): Unit = { |
| 63 | + val f0 = x.fun0(_) |
| 64 | + assert(f0(2) == 2) |
26 | 65 |
|
27 |
| - val f1 = x.foo(1)(_) |
28 |
| - assert(f1(2) == 3) |
| 66 | + val f1 = x.fun0 _ |
| 67 | + assert(f1(2) == 2) |
29 | 68 |
|
30 |
| - val f2 = x.foo(1) _ |
| 69 | + val f2 = x.fun1(1)(_) |
31 | 70 | assert(f2(2) == 3)
|
32 | 71 |
|
33 |
| - val f3 = x.foo(1) |
| 72 | + val f3 = x.fun1(1) _ |
34 | 73 | assert(f3(2) == 3)
|
35 | 74 |
|
36 |
| - val f4 = x.baz |
37 |
| - assert(f4(1) == 1) |
| 75 | + val f4 = x.fun1(1) |
| 76 | + assert(f4(2) == 3) |
| 77 | + } |
| 78 | + |
| 79 | + def implicits(x: Foo) = { |
| 80 | + implicit val y = 2 |
| 81 | + assert(x.fun4 == 2) |
| 82 | + assert(x.fun5(1) == 3) |
| 83 | + } |
| 84 | + |
| 85 | + // Limited support for dependant methods |
| 86 | + def dependant(x: Foo) = { |
| 87 | + val y = new D |
| 88 | + |
| 89 | + assert(x.fun6(y, "Hello") == 1) |
| 90 | + // assert(x.fun7(y, 1) == 2) // error: No ClassTag available for x.I |
| 91 | + |
| 92 | + val s = x.fun8(y) |
| 93 | + assert((s: String) == "Hello") |
38 | 94 |
|
39 |
| - val f5 = x.baz _ |
40 |
| - assert(f5(2) == 2) |
| 95 | + // val i = x.fun9(y) // error: rejected (blows up in pickler if not rejected) |
| 96 | + // assert((i: String) == "Hello") // error: Type mismatch: found: y.S(i); required: String |
41 | 97 | }
|
42 | 98 |
|
43 | 99 | def main(args: Array[String]): Unit = {
|
44 |
| - test(new FooI) |
| 100 | + basic(new FooI) |
| 101 | + currying(new FooI) |
| 102 | + etaExpansion(new FooI) |
| 103 | + implicits(new FooI) |
| 104 | + dependant(new FooI) |
45 | 105 | }
|
46 | 106 | }
|
0 commit comments