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,38 @@ 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. More precisely, it will will assign the `tracked` member the infered
134
+
type of the rhs. For instance, consider the following definition:
135
+
136
+
```scala
137
+
traitF:
138
+
tracked vala:Int
139
+
tracked valb:Int
140
+
141
+
classNextendsF:
142
+
vala=22// a.type =:= 22
143
+
valb:Int=22// b.type =:= Int
144
+
tracked valc=22// c.type =:= 22
145
+
```
146
+
147
+
Here, the `tracked` modifier ensures that the type of `a` in `N` is `22` and not
148
+
`Int`. But the type of `b` is `N` is `Int` since it's explicitly declared as
149
+
`Int`. `tracked` members can also be immediately initialized, as in the case of
150
+
`c`.
151
+
152
+
## Tracked syntax change
153
+
154
+
```
155
+
LocalModifier ::= ‘tracked’
156
+
```
157
+
158
+
The (soft) `tracked` modifier is allowed as a local modifier.
159
+
160
+
137
161
## Allow Class Parents to be Refined Types
138
162
139
163
Since `tracked` parameters create refinements in constructor types,
0 commit comments