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/separation-checking.md
+33Lines changed: 33 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -235,3 +235,36 @@ val c = b += 3
235
235
```
236
236
This code is equivalent to functional append with `+`, and is at the same time more efficient since it re-uses the storage of the argument buffer.
237
237
238
+
## The `freeze` Wrapper
239
+
240
+
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
241
+
capture any capabilities. This can be achieved using the `freeze` wrapper.
242
+
243
+
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:
254
+
```scala
255
+
importcaps.freeze
256
+
257
+
valf:IArr[String] =
258
+
vala=Arr[String](2)
259
+
a(0) ="hello"
260
+
a(1) ="world"
261
+
freeze(a)
262
+
```
263
+
The `freeze` method is defined in `caps` like this:
264
+
```scala
265
+
deffreeze(consume x: Mutable): x.type= x
266
+
```
267
+
It consumes a value of `Mutable` type with arbitrary capture set (since any capture set conforms to the implied `{cap.rd}`). The actual signature of
268
+
`consume` declares that `x.type` is returned, but the actual return type after capture checking is special. Instead of `x.type` it is the underlying `Mutable` type with its top-level capture set
269
+
mapped to `{}`. Applications of `freeze` are safe only if separation checking is enabled.
0 commit comments