Recovering Capabilities¶
A recover expression lets you “lift” the reference capability of the result. A mutable reference capability (iso, trn, or ref) can become any reference capability, and an immutable reference capability (val or box) can become any immutable or opaque reference capability.
Why is this useful?¶
This most straightforward use of recover is to get an iso that you can pass to another actor. But it can be used for many other things as well, such as:
- Creating a cyclic immutable data structure. That is, you can create a complex mutable data structure inside a
recoverexpression, “lift” the resultingrefto aval. - “Borrow” an
isoas aref, do a series of complex mutable operations on it, and return it as anisoagain. - “Extract” a mutable field from an
isoand return it as aniso.
What does this look like?¶
The recover expression wraps a list of expressions and is terminated by an end, like this:
recover Array[String].create() end
This expression returns an Array[String] iso, instead of the usual Array[String] ref you would get. The reason it is iso and not any of the other mutable reference capabilities is because there is a default reference capability when you don’t specify one. The default for any mutable reference capability is iso and the default for any immutable reference capability is val.
Here’s a more complicated example from the standard library:
recover
var s = String((prec + 1).max(width.max(31)))
var value = x
try
if value == 0 then
s.push(table(0)?)
else
while value != 0 do
let index = ((value = value / base) - (value * base))
s.push(table(index.usize())?)
end
end
end
_extend_digits(s, prec')
s.append(typestring)
s.append(prestring)
_pad(s, width, align, fill)
s
end
That’s from format/_FormatInt. It creates a String ref, does a bunch of stuff with it, and finally returns it as a String iso.
You can also give an explicit reference capability:
let key = recover val line.substring(0, i).>strip() end
Here, substring creates a new string and strip removes whitespace from it. The chain operator .> gives you the string on which you called strip. The explicit recover val makes the result a String val.
How does this work?¶
Inside the recover expression, your code only has access to sendable values from the enclosing lexical scope. In other words, you can only use iso, val and tag things from outside the recover expression.
This means that when the recover expression finishes, any aliases to the result of the expression other than iso, val and tag ones won’t exist anymore. That makes it safe to “lift” the reference capability of the result of the expression.
If the recover expression could access non-sendable values from the enclosing lexical scope, “lifting” the reference capability of the result wouldn’t be safe. Some of those values could “leak” into an iso or val result, and result in data races.
Automatic receiver recovery¶
Can you call a ref method on an iso or trn variable? Normally a method call gives the method an alias of the receiver. An alias of an iso can only be a tag, and an alias of a trn can only give you read access through a box. Neither allows the method to write.
For some calls the compiler can temporarily give the method a ref to the receiver. This is called automatic receiver recovery. The compiler checks what can go into the method and what can come back out:
- It must be safe to store each argument in the receiver. The compiler checks the arguments you actually pass, so you can pass a
String valeven if the parameter type isString box. - If you use the result, its type must be safe to take out of the receiver. You can keep a
U64count. Keeping a writable reference to a field of an isolated receiver would let you keep changing the field after giving the receiver to another actor. If you discard the result, you keep no such reference.
When these checks pass, you can make the call and keep your original capability afterwards. For example:
let s = recover String end
s.append("hi")
Here, s is a String iso. The append method needs a ref receiver, but the string literal we pass is a val, and we discard the result of the call. That lets append change the string while we keep our iso reference to it.