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
Copy file name to clipboardExpand all lines: docs/docs/reference/dropped-features/this-qualifier.md
+20Lines changed: 20 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,26 @@ Previously, these modifiers were needed for
12
12
13
13
- avoiding the generation of getters and setters
14
14
- excluding code under a `private[this]` from variance checks. (Scala 2 also excludes `protected[this]` but this was found to be unsound and was therefore removed).
15
+
- avoiding the generation of fields, if a `private[this] val` is not accessed
16
+
by a class method.
15
17
16
18
The compiler now infers for `private` members the fact that they are only accessed via `this`. Such members are treated as if they had been declared `private[this]`. `protected[this]` is dropped without a replacement.
17
19
20
+
This change can in some cases change the semantics of a Scala program, since a
21
+
`private` val is no longer guaranteed to generate a field. The field
22
+
is omitted if
23
+
24
+
- the `val` is only accessed via `this`, and
25
+
- the `val` is not accessed from a method in the current class.
26
+
27
+
This can cause problems if a program tries to access the missing private field via reflection. The recommended fix is to declare the field instead to be qualified private with the enclosing class as qualifier. Example:
28
+
```scala
29
+
classC(x: Int):
30
+
private[C] valfield= x +1
31
+
// [C] needed if `field` is to be accessed through reflection
0 commit comments