Skip to content

Commit f3b4834

Browse files
committed
Add scopability an explicit principle
1 parent af9c2bc commit f3b4834

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

docs/docs/reference/other-new-features/safe-initialization.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ By _reasonable usage_, we include the following use cases (but not restricted to
109109

110110
## Principles and Rules
111111

112-
To achieve the goals, we uphold two fundamental principles:
113-
_stackability_ and _monotonicity_.
112+
To achieve the goals, we uphold three fundamental principles:
113+
_stackability_, _monotonicity_ and _scopability_.
114114

115115
Stackability means that objects are initialized in stack order: if the
116116
object `b` is created during the initialization of object `a`, then
@@ -146,6 +146,28 @@ typestate_ to ensure soundness in the presence of aliasing
146146
[1]. Otherwise, either soundness will be compromised or we have to
147147
disallow the usage of already initialized fields.
148148

149+
Scopability means that given any environment ρ (which are the value
150+
bindings for method parameters) for evaluating an expression `e`, the
151+
resulting value is either transitively initialized or it may only
152+
reach uninitialized objects that are reachable from ρ in the heap
153+
before the evaluation. Control effects such as exceptions break this
154+
property, as the following example shows:
155+
156+
``` scala
157+
class MyException(val b: B) extends Exception("")
158+
class A {
159+
val b = try { new B } catch { case myEx: MyException => myEx.b }
160+
println(b.a)
161+
}
162+
class B {
163+
throw new MyException(this)
164+
val a: Int = 1
165+
}
166+
```
167+
168+
In the code above, the control effect teleport the uninitialized value
169+
wrapped in an exception. In the implementation, we avoid the problem
170+
by ensuring that the values that are thrown must be transitively initialized.
149171

150172
With the established principles and design goals, following rules are imposed:
151173

0 commit comments

Comments
 (0)