Skip to content

Commit 0e453a1

Browse files
committed
Add a section about tracked inference to the modularity doc
1 parent 5e295b3 commit 0e453a1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/_docs/reference/experimental/modularity.md

+31
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,37 @@ ClsParam ::= {Annotation} [{Modifier | ‘tracked’} (‘val’ | ‘var’)]
116116

117117
The (soft) `tracked` modifier is only allowed for `val` parameters of classes.
118118

119+
**Tracked inference**
120+
121+
In some cases `tracked` can be infered and doesn't have to be written
122+
explicitly. A common such case is when a class parameter is referenced in the
123+
signatures of the public members of the class. e.g.
124+
```scala 3
125+
class OrdSet(val ord: Ordering) {
126+
type Set = List[ord.T]
127+
def empty: Set = Nil
128+
129+
implicit class helper(s: Set) {
130+
def add(x: ord.T): Set = x :: remove(x)
131+
def remove(x: ord.T): Set = s.filter(e => ord.compare(x, e) != 0)
132+
def member(x: ord.T): Boolean = s.exists(e => ord.compare(x, e) == 0)
133+
}
134+
}
135+
```
136+
In the example above, `ord` is referenced in the signatures of the public
137+
members of `OrdSet`, so a `tracked` modifier will be inserted automatically.
138+
139+
Another common case is when a context bound has an associated type (i.e. an abstract type member) e.g.
140+
```scala 3
141+
trait TC:
142+
type Self
143+
type T
144+
145+
class Klass[A: {TC as tc}]
146+
```
147+
148+
Here, `tc` is a context bound with an associated type `T`, so `tracked` will be inferred for `tc`.
149+
119150
**Discussion**
120151

121152
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:

0 commit comments

Comments
 (0)