Skip to content

Commit cc69c74

Browse files
committed
Add note on recursive lazy vals
1 parent 9e044a3 commit cc69c74

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

docs/docs/reference/changed/lazy-vals-spec.md

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ initialized by some thread. The state 2 denotes that there are concurrent reader
6969
The state 3 represents a lazy val that has been initialized. `<field-id>` is the id of the lazy
7070
val. This id grows with the number of volatile lazy vals defined in the class.
7171

72+
## Note on recursive lazy vals
73+
74+
Ideally recursive lazy vals should be flagged as an error. The current behavior for `@volatile`
75+
recursive lazy vals is undefined (initialization may result in a deadlock). Non `@volatile` lazy
76+
vals behave as in Scala 2.
77+
7278
## Reference
7379

7480
* [SIP-20]

tests/run/i1856.check

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Iteration 0
2+
Iteration 1
3+
Iteration 2
4+
Iteration 3
5+
Iteration 4
6+
Iteration 5
7+
Iteration 6
8+
Iteration 7
9+
Iteration 8
10+
Iteration 9
11+
42
12+
Iteration 0
13+
Iteration 1
14+
Iteration 2
15+
Iteration 3
16+
Iteration 4
17+
Iteration 5
18+
Iteration 6
19+
Iteration 7
20+
Iteration 8
21+
Iteration 9
22+
42

tests/run/i1856.scala

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
object Test {
2+
var count = 0
3+
lazy val lzy: Int = {
4+
if (count < 10) {
5+
println(s"Iteration $count")
6+
count += 1
7+
lzy
8+
} else 42
9+
}
10+
11+
def lzy2 = {
12+
var countLocal = 0
13+
lazy val lzyLocal: Int = {
14+
if (countLocal < 10) {
15+
println(s"Iteration $countLocal")
16+
countLocal += 1
17+
lzyLocal
18+
} else 42
19+
}
20+
lzyLocal
21+
}
22+
23+
def main(args: Array[String]): Unit = {
24+
println(lzy)
25+
println(lzy2)
26+
}
27+
}

0 commit comments

Comments
 (0)