File tree 2 files changed +74
-0
lines changed
2 files changed +74
-0
lines changed Original file line number Diff line number Diff line change
1
+ class A { class E }
2
+ class B extends A { class E }
3
+ trait C { type E = Int }
4
+ trait D { type E = String }
5
+ trait EC { type E }
6
+
7
+ object Test {
8
+ // should not compile (?)
9
+ // martin says "I'd argue about that"
10
+ // martin retracts his statement: this should not compile
11
+ type EE [+ X <: EC ] = X # E
12
+ type EE2 [+ X <: EC ] = X # E // repeat to get error count to 2
13
+
14
+ def fail1 (): Unit = {
15
+ val b = new B
16
+ var x1 : EE [A ] = null
17
+ var x2 : EE [B ] = new b.E // error: found: B#E, required: A#E
18
+ // x1 = x2 // gives a prior type error: B#E, required: A#E, masked to get at the real thing.
19
+ }
20
+
21
+ /* Not representable in dotty as there are no existential types
22
+ def fail2(): Unit = {
23
+ val b = new B
24
+ var x1: p.E forSome { val p: A } = new b.E // should not compile
25
+ var x2: p.E forSome { val p: B } = new b.E
26
+ x1 = x2 // should not compile
27
+ }
28
+ */
29
+ def fail3 (): Unit = {
30
+ var x1 : EE [C ] = 5
31
+ var x2 : EE [C & D ] = " "
32
+ x1 = x2
33
+ }
34
+
35
+ def wrap (label : String )(op : => Unit ): Unit =
36
+ try { op } catch { case x : ClassCastException => println(" %30s %s" .format(label, x)) }
37
+
38
+ def main (args : Array [String ]): Unit = {
39
+ wrap(" Variance and inner classes" )(fail1())
40
+ wrap(" Linearization and type aliases" )(fail3())
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ class Foo {
2
+ class Bar {
3
+ def foo = 1
4
+ }
5
+ }
6
+
7
+ class SubFoo extends Foo {
8
+ class SubBar {
9
+ def foo = 42
10
+ }
11
+ }
12
+
13
+ object Test {
14
+ def main (args : Array [String ]): Unit = {
15
+
16
+ // Let's create some instances:
17
+ val foo = new Foo
18
+ val fooBar = new foo.Bar
19
+ assert(fooBar.foo == 1 ) // > res0: Int = 1
20
+ // ok
21
+
22
+ val subFoo = new SubFoo
23
+ val subFooBar = new subFoo.SubBar
24
+ assert(subFooBar.foo == 42 ) // > res1: Int = 42
25
+ // ok
26
+
27
+ val superFoo : Foo = subFoo
28
+ val superFooBar = new superFoo.Bar
29
+ assert(superFooBar.foo == 1 ) // > res2: Int = 1
30
+ // NOT OK!
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments