Skip to content

Commit af631d5

Browse files
committed
Test case
This demonstrates currently unsoundness when it comes to assignments via setters
1 parent b98efc4 commit af631d5

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.*
2+
3+
class Ref[T](init: T):
4+
var x: T = init
5+
def setX(x: T): Unit = this.x = x
6+
7+
def usingLogFile[sealed T](op: FileOutputStream^ => T): T =
8+
val logFile = FileOutputStream("log")
9+
val result = op(logFile)
10+
logFile.close()
11+
result
12+
13+
type Proc = () => Unit
14+
def test1 =
15+
usingLogFile[Proc]: f => // error
16+
() =>
17+
f.write(1)
18+
()
19+
20+
def test2 =
21+
val r = new Ref[Proc](() => ())
22+
usingLogFile[Unit]: f =>
23+
r.setX(() => f.write(10)) // should be error
24+
r.x() // crash: f is closed at that point
25+
26+
def test3 =
27+
val r = new Ref[Proc](() => ())
28+
usingLogFile[Unit]: f =>
29+
r.x = () => f.write(10) // should be error
30+
r.x() // crash: f is closed at that point
31+
32+
def test4 =
33+
var r: Proc = () => () // error
34+
usingLogFile[Unit]: f =>
35+
r = () => f.write(10)
36+
r() // crash: f is closed at that point
37+
38+
39+
40+
41+
42+

0 commit comments

Comments
 (0)