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
The (soft) `tracked` modifier is only allowed for `val` parameters of classes.
118
-
119
111
**Discussion**
120
112
121
113
Since `tracked` is so useful, why not assume it by default? First, `tracked` makes sense only for `val` parameters. If a class parameter is not also a field declared using `val` then there's nothing to refine in the constructor result type. One could think of at least making all `val` parameters tracked by default, but that would be a backwards incompatible change. For instance, the following code would break:
@@ -134,6 +126,37 @@ only if the class refers to a type member of `x`. But it turns out that this
134
126
scheme is unimplementable since it would quickly lead to cyclic references
135
127
when typechecking recursive class graphs. So an explicit `tracked` looks like the best available option.
136
128
129
+
## Tracked members
130
+
131
+
The `tracked` modifier can also be used for `val` members of classes and traits
132
+
to force the type of the member (or it's overriding member) to be as exact as
133
+
possible. For instance, consider the following definition:
134
+
135
+
```scala
136
+
traitF:
137
+
tracked vala:Int
138
+
tracked valb:Int
139
+
140
+
classNextendsF:
141
+
vala=22// a.type =:= 22
142
+
valb:Int=22// b.type =:= Int
143
+
tracked valc=22// c.type =:= 22
144
+
```
145
+
146
+
Here, the `tracked` modifier ensures that the type of `a` in `N` is `22` and not
147
+
`Int`. But the type of `b` is `N` is `Int` since it's explicitly declared as
148
+
`Int`. `tracked` members can also be immediately initialized, as in the case of
149
+
`c`.
150
+
151
+
## Tracked syntax change
152
+
153
+
```
154
+
LocalModifier ::= ‘tracked’
155
+
```
156
+
157
+
The (soft) `tracked` modifier is allowed as a local modifier.
158
+
159
+
137
160
## Allow Class Parents to be Refined Types
138
161
139
162
Since `tracked` parameters create refinements in constructor types,
0 commit comments