Skip to content

Commit

Permalink
Add a section about tracked inference to the modularity doc
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Nov 18, 2024
1 parent 5c11bfb commit 734f7a2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/_docs/reference/experimental/modularity.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,37 @@ ClsParam ::= {Annotation} [{Modifier | ‘tracked’} (‘val’ | ‘var’)]

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

**Tracked inference**

In some cases `tracked` can be infered and doesn't have to be written
explicitly. A common such case is when a class parameter is referenced in the
signatures of the public members of the class. e.g.
```scala 3
class OrdSet(val ord: Ordering) {
type Set = List[ord.T]
def empty: Set = Nil

implicit class helper(s: Set) {
def add(x: ord.T): Set = x :: remove(x)
def remove(x: ord.T): Set = s.filter(e => ord.compare(x, e) != 0)
def member(x: ord.T): Boolean = s.exists(e => ord.compare(x, e) == 0)
}
}
```
In the example above, `ord` is referenced in the signatures of the public
members of `OrdSet`, so a `tracked` modifier will be inserted automatically.

Another common case is when a context bound has an associated type (i.e. an abstract type member) e.g.
```scala 3
trait TC:
type Self
type T

class Klass[A: {TC as tc}]
```

Here, `tc` is a context bound with an associated type `T`, so `tracked` will be inferred for `tc`.

**Discussion**

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:
Expand Down

0 comments on commit 734f7a2

Please sign in to comment.