File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ class A (o : O ):
2+ var a = 20
3+
4+ class B (o : O ):
5+ var b = 20
6+
7+ class O :
8+ val o : A | B = new A (this )
9+ if o.isInstanceOf [A ] then
10+ o.asInstanceOf [A ].a += 1
11+ else
12+ o.asInstanceOf [B ].b += 1 // o.asInstanceOf[B] is treated as bottom
13+
14+ // prevent early promotion
15+ val x = 10
Original file line number Diff line number Diff line change 1+ class A (c : C ):
2+ val f : Int = 10
3+ def m () = f
4+
5+ class B (c : C ):
6+ val f : Int = g() // warn
7+ def g (): Int = f
8+
9+ class C (x : Int ):
10+ val a : A | B = if x > 0 then new A (this ) else new B (this )
11+
12+ def cast [T ](a : Any ): T = a.asInstanceOf [T ]
13+
14+ val c : A = a.asInstanceOf [A ] // abstraction for c is {A, B}
15+ val d = c.f // treat as c.asInstanceOf[owner of f].f
16+ val e = c.m() // treat as c.asInstanceOf[owner of f].m()
17+ val c2 : B = a.asInstanceOf [B ]
18+ val g = c2.f // no error here
19+
You can’t perform that action at this time.
0 commit comments