Skip to content

Commit 3dd016a

Browse files
authored
Merge pull request #60 from ktoso/actors
[Actors] explain nonisoalted actor func + isolated param a bit more
2 parents ed53bd8 + 40e6d0b commit 3dd016a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

proposals/nnnn-actors.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func investWildly(account: BankAccount, amount: Double) async {
206206
account.balance += winnings
207207
}
208208
}
209-
```
209+
```
210210

211211
It also allows actor functions to be extracted into global or local functions, so code can be refactored without breaking actor isolation. Due to `isolated` parameters, the `self` parameter of an actor function is actually not that special: it merely defaults to `isolated` because of the context in which it is declared. For example, type of `BankAccount.deposit(amount:)`, defined above, is a curried function that involves an isolated `self`:
212212

@@ -612,6 +612,35 @@ By default, the mutable instance stored properties (declared with `var`) of an a
612612

613613
> **Rationale**: `nonisolated(unsafe)` allows specific stored instance properties to opt out of actor isolation checking, allowing careful developers to implement their own synchronization mechanisms.
614614
615+
It is legal, albeit uncommon, to declare an actor instance method as `nonisolated` _and_ have it accept a different `isolated` actor parameter. Since nonisolated can be understood as removing the implicit `isolated self`, the addition of an `isolated` parameter still properly respects the requirement that there be only a single actor with which a function is isolated.
616+
617+
In other words, the following function:
618+
619+
```swift
620+
actor Greeter {
621+
func greet(_ name: String) { ... }
622+
}
623+
624+
actor Worker {
625+
private let name: String
626+
var creditCard: CreditCard
627+
628+
nonisolated func hello(by greeter: isolated Greeter) {
629+
greeter.greet(self.name)
630+
greeter.take(creditCard) // error: nonisolated Worker function cannot refer to isoalted state 'creditCard', function is actor-isolated to 'greeter: isolated Greeter'
631+
}
632+
}
633+
```
634+
635+
Exhibits the following semantics:
636+
637+
- this function is isolated with the `Greeter` actor, and *not* the `Worker` (!),
638+
- as such, it can synchronously invoke the `greeter.greet` function.
639+
- it cannot refer to isolated state of the Worker,
640+
- it will execute on the `Greeter` actor.
641+
642+
In practice this means that this function can only be invoked by a greeter which passes `self` as an isolated parameter to the hello function of the worker it is about to greet. The `hello` function can refer to the actors private, non isolated state, such as a constant (or other `nonisolated` variables).
643+
615644
### Actor isolation checking
616645

617646
Any given declaration in a program is either isolated to a specific actor or is non-isolated.

0 commit comments

Comments
 (0)