You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To understand the problem, let's pick the following concrete example.
10
+
The following example illustrates the problem:
11
11
12
12
abstract class A {
13
13
val x1: String
@@ -26,7 +26,7 @@ To understand the problem, let's pick the following concrete example.
26
26
println("C: " + x1 + ", " + x2)
27
27
}
28
28
29
-
Let's observe the initialization order through the Scala REPL:
29
+
In the Scala REPL we observe:
30
30
31
31
scala> new C
32
32
A: null, null
@@ -36,129 +36,125 @@ Let's observe the initialization order through the Scala REPL:
36
36
Only when we get to the constructor of `C` are both `x1` and `x2` initialized. Therefore, constructors of `A` and `B` risk running into `NullPointerException`s.
37
37
38
38
## Explanation
39
-
A 'strict' or 'eager' val is one which is not marked lazy.
40
39
41
-
In the absence of "early definitions" (see below), initialization of strict vals is done in the following order.
40
+
A "strict" or "eager" val is one which is not marked lazy.
41
+
Initialization of strict vals is done in the following order:
42
42
43
43
1. Superclasses are fully initialized before subclasses.
44
44
2. Otherwise, in declaration order.
45
45
46
-
Naturally when a val is overridden, it is not initialized more than once. So though x2 in the above example is seemingly defined at every point, this is not the case: an overridden val will appear to be null during the construction of superclasses, as will an abstract val.
46
+
When a `val` is overridden, in fact its accessor method (the "getter") is overridden.
47
+
So the access to `x2` in class `A` in fact invokes the overridden getter in class `C` which reads the underlying field `C.x2`.
48
+
This field is not yet initialized during the construction of `A`.
47
49
48
-
There is a compiler flag which can be useful for identifying this situation:
50
+
## Mitigation
49
51
50
-
**-Xcheckinit**: Add runtime check to field accessors.
52
+
The [`-Ysafe-init` compiler flag](https://docs.scala-lang.org/scala3/reference/other-new-features/safe-initialization.html) in Scala 3 enables compiler warnings for accesses to uninitialized fields:
51
53
52
-
It is inadvisable to use this flag outside of testing. It adds significantly to the code size by putting a wrapper around all potentially uninitialized field accesses: the wrapper will throw an exception rather than allow a null (or 0/false in the case of primitive types) to silently appear. Note also that this adds a *runtime* check: it can only tell you anything about code paths which you exercise with it in place.
Usually the best answer. Unfortunately you cannot declare an abstract lazy val. If that is what you're after, your options include:
90
+
Values passed as parameters to the superclass constructor are available in its body.
92
91
93
-
1. Declare an abstract strict val, and hope subclasses will implement it as a lazy val or with an early definition. If they do not, it will appear to be uninitialized at some points during construction.
94
-
2. Declare an abstract def, and hope subclasses will implement it as a lazy val. If they do not, it will be re-evaluated on every access.
95
-
3. Declare a concrete lazy val which throws an exception, and hope subclasses override it. If they do not, it will... throw an exception.
92
+
Scala 3 also [supports trait parameters](https://docs.scala-lang.org/scala3/reference/other-new-features/trait-parameters.html).
96
93
97
-
An exception during initialization of a lazy val will cause the right-hand side to be re-evaluated on the next access: see SLS 5.2.
94
+
Note that overriding a `val` class parameter is deprecated / disallowed in Scala 3.
95
+
Doing so in Scala 2 can lead to surprising behavior.
98
96
99
-
Note that using multiple lazy vals creates a new risk: cycles among lazy vals can result in a stack overflow on first access.
97
+
### Use lazy vals
100
98
101
-
#### Use early definitions ####
102
99
abstract class A {
103
-
val x1: String
104
-
val x2: String = "mom"
100
+
lazy val x1: String
101
+
lazy val x2: String = "mom"
105
102
106
103
println("A: " + x1 + ", " + x2)
107
104
}
108
-
class B extends {
109
-
val x1: String = "hello"
110
-
} with A {
105
+
class B extends A {
106
+
lazy val x1: String = "hello"
107
+
111
108
println("B: " + x1 + ", " + x2)
112
109
}
113
-
class C extends {
114
-
override val x2: String = "dad"
115
-
} with B {
110
+
class C extends B {
111
+
override lazy val x2: String = "dad"
112
+
116
113
println("C: " + x1 + ", " + x2)
117
114
}
118
115
// scala> new C
119
116
// A: hello, dad
120
117
// B: hello, dad
121
118
// C: hello, dad
122
119
123
-
Early definitions are a bit unwieldy, there are limitations as to what can appear and what can be referenced in an early definitions block, and they don't compose as well as lazy vals: but if a lazy val is undesirable, they present another option. They are specified in SLS 5.1.6.
120
+
Note that abstract `lazy val`s are supported in Scala 3, but not in Scala 2.
121
+
In Scala 2, you can define an abstract `val` or `def` instead.
124
122
125
-
Note that early definitions are deprecated in Scala 2.13; they will be replaced by trait parameters in Scala 3. So, early definitions are not recommended for use if future compatibility is a concern.
123
+
An exception during initialization of a lazy val will cause the right-hand side to be re-evaluated on the next access: see SLS 5.2.
126
124
127
-
#### Use constant value definitions ####
128
-
abstract class A {
129
-
val x1: String
130
-
val x2: String = "mom"
125
+
Note that using multiple lazy vals creates a new risk: cycles among lazy vals can result in a stack overflow on first access.
131
126
132
-
println("A: " + x1 + ", " + x2)
133
-
}
134
-
class B extends A {
135
-
val x1: String = "hello"
136
-
final val x3 = "goodbye"
127
+
### Use a nested object
137
128
138
-
println("B: " + x1 + ", " + x2)
139
-
}
140
-
class C extends B {
141
-
override val x2: String = "dad"
129
+
Sometimes, uninitialized state in a subclass is accessed during construction of a superclass:
0 commit comments