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/experimental/capture-checking/mutability.md
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -444,3 +444,35 @@ The subcapturing theory for sets is then as before, with the following additiona
444
444
-`C₁.reader <: C₂.reader` if `C₍ <: C₂`
445
445
-`{x, ...}.reader = {x.rd, ...}.reader`
446
446
-`{x.rd, ...} <: {x, ...}`
447
+
448
+
## The `freeze` Wrapper
449
+
450
+
We often want to create a mutable data structure like an array, initialize by assigning to its elements and then return the array as an immutable type that does not
451
+
capture any capabilities. This can be achieved using the `freeze` wrapper.
452
+
453
+
As an example, consider a class `Arr` which is modelled after `Array` and its immutable counterpart `IArr`:
The `freeze` wrapper allows us to go from an `Arr` to an `IArr`, safely:
464
+
```scala
465
+
importcaps.freeze
466
+
467
+
valf:IArr[String] =freeze:
468
+
vala=Arr[String](2)
469
+
a(0) ="hello"
470
+
a(1) ="world"
471
+
a
472
+
```
473
+
The `freeze` method is defined in `caps` like this:
474
+
```scala
475
+
deffreeze[T](op: ->T):T= op
476
+
```
477
+
It takes a pure by-name parameter and returns its result. But the actual return type after capture checking is special. Instead of just `T` as in the declaration above suggests, it's `T` where every covariant occurrence of a `Mutable` type gets its capture set mapped to `{}`.
0 commit comments