forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scala#8861: Avoid parameters when instantiating closure results
- Loading branch information
Showing
2 changed files
with
42 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
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,31 @@ | ||
object Test { | ||
sealed trait Container { s => | ||
type A | ||
def visit[R](int: IntV & s.type => R, str: StrV & s.type => R): R | ||
} | ||
final class IntV extends Container { s => | ||
type A = Int | ||
val i: Int = 42 | ||
def visit[R](int: IntV & s.type => R, str: StrV & s.type => R): R = int(this) | ||
} | ||
final class StrV extends Container { s => | ||
type A = String | ||
val t: String = "hello" | ||
def visit[R](int: IntV & s.type => R, str: StrV & s.type => R): R = str(this) | ||
} | ||
|
||
def minimalOk[R](c: Container { type A = R }): R = c.visit[R]( | ||
int = vi => vi.i : vi.A, | ||
str = vs => vs.t : vs.A | ||
) | ||
def minimalFail[M](c: Container { type A = M }): M = c.visit( | ||
int = vi => vi.i : vi.A, | ||
str = vs => vs.t : vs.A // error | ||
) | ||
|
||
def main(args: Array[String]): Unit = { | ||
val e: Container { type A = String } = new StrV | ||
println(minimalOk(e)) // this one prints "hello" | ||
println(minimalFail(e)) // this one fails with ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer | ||
} | ||
} |