-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ab198d
commit f75f6dc
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class A(o: O): | ||
var a = 20 | ||
|
||
class B(o: O): | ||
var b = 20 | ||
|
||
class O: | ||
val o: A | B = new A(this) | ||
if o.isInstanceOf[A] then | ||
o.asInstanceOf[A].a += 1 | ||
else | ||
o.asInstanceOf[B].b += 1 // o.asInstanceOf[B] is treated as bottom | ||
|
||
// prevent early promotion | ||
val x = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class A(c: C): | ||
val f: Int = 10 | ||
def m() = f | ||
|
||
class B(c: C): | ||
val f: Int = g() // warn | ||
def g(): Int = f | ||
|
||
class C(x: Int): | ||
val a: A | B = if x > 0 then new A(this) else new B(this) | ||
|
||
def cast[T](a: Any): T = a.asInstanceOf[T] | ||
|
||
val c: A = a.asInstanceOf[A] // abstraction for c is {A, B} | ||
val d = c.f // treat as c.asInstanceOf[owner of f].f | ||
val e = c.m() // treat as c.asInstanceOf[owner of f].m() | ||
val c2: B = a.asInstanceOf[B] | ||
val g = c2.f // no error here | ||
|