# \[Pitch #4\] Actors

**URL:** <https://forums.swift.org/t/pitch-4-actors/45215>\
**Category:** Pitches\
**Created:** [March 2, 2021, 7:55am UTC](https://forums.swift.org/t/pitch-4-actors/45215 "2021-03-02T07:55:32Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [March 4, 2021, 8:16am UTC](https://forums.swift.org/t/pitch-4-actors/45215/13 "2021-03-04T08:16:13Z")

</div>

> [@Chris\_Lattner3](#):
>
> > - Allow cross-actor references to actor properties, so long as they are reads (not writes or `inout` references)
> 
> Got it, I assume the challenge here is in promoting setters to async? Do you anticipate that this is difficult to add in the future, or just "non-critical" for now? It would be weird if the final model allows one without the other.

There is going to be "weird" somewhere with async access to properties. This proposal draws the line at setting. If we allowed setting, we would draw the line at `inout`. I also feel like piece-meal setting of actor state property-by-property might be something we don't want to support long-term, so this felt safer.

> [@Chris\_Lattner3](#):
>
> It's completely a wording/positioning thing, but I'd suggest changing the discussion to be less about isolation/data races (structured concurrency provides the same benefits) and more something that talks about abstraction and design patterns.

This is a good idea. I've incorporated much of this, thanks!

> [@Chris\_Lattner3](#):
>
> Super nit, but I'd recommend dropping the `private` markers from [the lead example](https://github.com/DougGregor/swift-evolution/blob/actors/proposals/nnnn-actors.md#actors-1) of actors. They are unnecessary and a bit distracting now, since they syntactically overpower the let/var declarations that matter.

Sure, they're gone.

> [@Chris\_Lattner3](#):
>
> In the cross actor reference discussion at the end of [this section](https://github.com/DougGregor/swift-evolution/blob/actors/proposals/nnnn-actors.md#actor-isolation) it would be good to mention some rationale for the decisions - why aren't cross-actor setters allowed? I think I know, but it would be good to make the proposal self contained and obvious to other readers.

I've added some rationale.

> [@Chris\_Lattner3](#):
>
> Regardless of the keyword, are optionals and IUO's allowed here? e.g. `func thing(x: isolated BankAccount?)` and `func thing(x: isolated BankAccount!)`

No, I don't think so. We could add it later if it's important.

> [@Chris\_Lattner3](#):
>
> Is it possible to pull the `runOnActor` method into a protocol extension on `AnyActor` so it is available everywhere?

@ktoso was arguing that it's not an API we should actually provide. I'll let him give his reasons.

> [@Chris\_Lattner3](#):
>
> "Therefore, we prohibit the definition of a function with more than one isolated parameter:" --\> Why? This can be narrowly useful (e.g. unsafe casts when two actor instances are known to be on the same executor) and there is no reason to ban it. Why exclude a (narrowly useful) valid case and add the compiler complexity? I don't see any safety benefit to this.

Yes, I agree that it would be possible to call such a thing by, say, unsafely casting the function type. The restriction is there to point out a potential pitfall... but I'm okay with removing it. Perhaps at some point we'll gain a way to have multiple actor instances that are statically known to share a concurrency domain/executor.

> [@Chris\_Lattner3](#):
>
> About the feature itself, I think it would be much better if you motivate nonisolated without using protocol conformance as the rationale.

I'm fine with introducing additional motivation first, and the some reshuffling to bring it later, but `nonisolated` is directly related to `isolated` and has proven to be an important part of the model, so it will remain in this proposal. I've gone ahead and done this.

> [@Chris\_Lattner3](#):
>
> Nit: The closure isn't non-isolated. It is `@concurrent` or `@sendable` or whatever:

Sure, fixed.

> [@Chris\_Lattner3](#):
>
> Overall I'm curious if this is actually required now that the other changes have gone into the model. The original concern was about GCD and other APIs that bottom out in it, but won't those be marked with Clang attributes as taking `@concurrent` closures?

Yes, this is absolutely required. There is no code with correctly-annotated `@concurrent` functions yet, and we can not instantaneously update the world to introduce the appropriate `@concurrent` annotations. We can start lifting this restriction from code that's getting recompiled under the structured Swift Concurrency model, but it will take time.

> [@Chris\_Lattner3](#):
>
> I don't think this is true, there are lots of reasons to use `@escaping` closures other than concurrency, including basic callback things, main thread UI things, etc.

Lots of basic callback things happen "later" and would introduce data races, and the main thread is particularly important for safety.

> [@Chris\_Lattner3](#):
>
> Also keep in mind that this isn't actually solving the problem as [Dispatch.sync](https://developer.apple.com/documentation/dispatch/dispatchqueue/1452870-sync) and other concurrency APIs don't take escaping closures.

Dispatch.sync is semantically fine (albeit a performance nightmare), because the caller is blocked. You want [concurrentPerform](https://developer.apple.com/documentation/dispatch/dispatchqueue/2016088-concurrentperform) to prove your point, and yes: the `@escaping` approach described in this proposal is not 100% bulletproof. But experience with the model shows that it is extremely valuable in catching concurrent executions when working with the Swift ecosystem we have today.

We can talk about more effective ways to phase out this `@escaping` or time, but I'll be blunt: that `@escaping` implies non-isolated capture is not negotiable.

> [@Chris\_Lattner3](#):
>
> ## [inout parameters](https://github.com/DougGregor/swift-evolution/blob/actors/proposals/nnnn-actors.md#inout-parameters) section
> 
> This limitation is written to apply the to properties in actors, but this limitation should really apply to any state accessible from the actor,

Sure, thanks for the example.

> [@Chris\_Lattner3](#):
>
> Wording nit: I'd replace "A separate proposal introduces the ConcurrentValue protocol" with "SE-0302 introduces the ConcurrentValue marker protocol"

... or `Sendable`, as it were. Done.

> [@Chris\_Lattner3](#):
>
> ## [Actor Isolation Checking](https://github.com/DougGregor/swift-evolution/blob/actors/proposals/nnnn-actors.md#actor-isolation-checking) section
> 
> Writing suggestion: I think it would be best to rethink the presentation of this section in general. It came from historical roots, but now has a bunch of stuff (incl how protocol conformance works) that have little to do with isolation and relate to the overall design of actors as a nominal type. I'd recommend changing the "detailed design" section to just list out how all the language features work (not as subsections of isolation) and then have actor isolation be a small peer section that describes the limitations imposed by it.
> 
> Furthermore, the section is conflating two different things: 1) how actor isolation works in the general model, and 2) various details of the additive and separable "nonisolated" language feature. For example, the "overrides" section is related to the later not the former.

I can look into improving the presentation here, but I think your suggestions here are all deeply influenced by your feeling that `nonisolated` is unimportant and should not be part of the model.

> [@Chris\_Lattner3](#):
>
> Partial application of a non-isolated actor reference to a method produces an async function type: `otherA.f -> (Int)->Double async`

I see what you mean here; it's effectively applying the principle that one can asynchronously access synchronous methods to partial applications. This can be made to work, although it runs afoul of one of the principles we've maintained thus far, which is that the type of a reference to a declaration (e.g., the type of `otherA.f`) is not dependent on whether `otherA` is isolated or not in this context.

> [@Chris\_Lattner3](#):
>
> You can't pass `self.g` to runLater because (some rules that I don't actually understand w.r.t. escaping).

The rules here from the need to not escape isolated actor instances. Remember that `self.g` is `@escaping` when there is no context forcing it to be noescape, e.g.,

```swift
let fn = self.g

```

produces an `@escaping` function type.

> [@Chris\_Lattner3](#):
>
> ## [Isolated Protocol Conformances](https://github.com/DougGregor/swift-evolution/blob/actors/proposals/nnnn-actors.md#isolated-protocol-conformances) in future directions
> 
> The discussion here is a reasonable framing, but I think you have the sense of protocol conformance wrong. This draft of the proposal is suggesting that:
> 
> - `actor Foo : SomeProtocol` means a "non-isolated" conformance
> - `actor Foo : isolated SomeProtocol` means an "isolated" conformance
> 
> However, non-isolated functionality is much more limited (and no core to the actors design as I argue above) than isolated functionality. I think instead you should have:
> 
> - `actor Foo : SomeProtocol` means an "isolated" conformance
> - `actor Foo : nonisolated SomeProtocol` means a "nonisolated" conformance
> 
> This also allows you to move the "nonisolated" feature in general out to a followup, focusing this proposal on building out the core actor model.

Isolated conformances are the ones that are different from any other kind of conformance we have in the type system, because they only apply to a subset of the values of a given type, and necessarily have other restrictions on what kinds of protocols can work with them. Non-isolated conformances apply to every instance of the type, like all other conformances in Swift. One of the two must have a qualifier, and `isolated` conformances---which work only on `isolated` parameters and are different from all other conformances---should be have the qualifier.

> [@Chris\_Lattner3](#):
>
> I didn't see it in this proposal, but in previous versions of the draft there was the suggestion of having an `AnyActor` like protocol that all actors conform to, as an analog to [`AnyClass`](https://developer.apple.com/documentation/swift/anyclass). Is this still interesting and inflight or did this get intentionally dropped?

It was called simply `Actor`, and we dropped it at one point because one couldn't actually do anything with it. However, I'd like to bring it back as an empty protocol (to be filled in by the custom executors proposal) to reserve the name and give a place where we can hang the `Sendable` conformance. If we add `runOnActor`, that's where we would do it.

Doug

---

_[View the full topic](https://forums.swift.org/t/pitch-4-actors/45215)._
