# \[Pitch #2\] On Actors and Initialization

**URL:** <https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972>\
**Category:** Pitches\
**Created:** [December 10, 2021, 6:10pm UTC](https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972 "2021-12-10T18:10:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kavon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kavon/32/9774_2.png) [@kavon](https://forums.swift.org/u/kavon)\
**Post date:** [December 10, 2021, 6:10pm UTC](https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972/1 "2021-12-10T18:10:34Z")

</div>

Hi folks,

I've got an [all-new proposal](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md) to fix how actor initialization works, based on feedback from the first review. There are also changes to actor initializer delegation, stored property isolation, and deinitializers.

Since the proposal differs significantly from the first review, this is a pitch. Here's a very informal and incomplete summary of the major functionality proposed, along with some links into the document itself for more details:

1. Actor initializers that are not isolated to the actor will now allow you to do anything you normally can from a `nonisolated` method. In exchange, Swift will automatically reject accesses to stored properties that might be unsafe. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#overly-restrictive-non-async-initializers) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#initializers-with-nonisolated-self).
2. Deinitializers of an actor can no longer access non-sendable stored properties of the instance. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#data-races-in-deinitializers) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#deinitializers)
3. A type's stored property cannot have a default value if its isolation is not compatible with its initializers. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#stored-property-isolation) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#global-actor-isolation-and-instance-members)
4. The `convenience` keyword is no longer required to define a delegating initializer of an actor. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#initializer-delegation) and [proposed rules](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#delegating-initializers) for their delegating initializers, which is continued in the Sendability section.

The proposal is lengthy, but I've tried to always follow each discussion with an example, and have isolated the more esoteric discussions under "In-depth Discussion" headings at the end of some sections. As always, I greatly appreciate any time you spend helping to improve Swift by reading this proposal!

---

<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:** [December 14, 2021, 8:45pm UTC](https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972/2 "2021-12-14T20:45:37Z")

</div>

> [@kavon](#):
>
> Actor initializers that are not isolated to the actor will now allow you to do anything you normally can from a `nonisolated` method. In exchange, Swift will automatically reject accesses to stored properties that might be unsafe. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#overly-restrictive-non-async-initializers) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#initializers-with-nonisolated-self).

IIUC, this is the "control-flow-based" checking we discussed in the first review. I like that it aligns (synchronous) initializers with `nonisolated` methods, so it's easier to reason about. And if we eventually get non-escaping parameters, one will be able to do a lot more in initializers for free.

> [@kavon](#):
>
> Deinitializers of an actor can no longer access non-sendable stored properties of the instance. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#data-races-in-deinitializers) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#deinitializers)

Yeah, this makes sense.

> [@kavon](#):
>
> A type's stored property cannot have a default value if its isolation is not compatible with its initializers. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#stored-property-isolation) and [proposed solution](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#global-actor-isolation-and-instance-members)

I'm not sure the complexity of this solution is worth it. The solution asks us to consider all of the initializers:

> For a given type with some stored property, a default value can only be provided if the isolation of the property is _compatible_ with the isolation of all non-async and non-delegating initializers of that type. An isolated property is _compatible_ if it is not isolated, or all such initializers have the same isolation.

... but the initializers might be scattered among different source files, and we have to look at the initializer bodies to determine whether they are delegating or not (now that `convenience` is not required), which implies a lot of work for the compiler. How bad would it be if we said that the default values of stored properties in actors have to be evaluated as if in a `nonisolated` context? And the same for default values of stored properties in global-actor-constrained structs/classes/enums?

> [@kavon](#):
>
> The `convenience` keyword is no longer required to define a delegating initializer of an actor. Here is the [problem description](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#initializer-delegation) and [proposed rules](https://github.com/kavon/swift-evolution/blob/actor-initializers-review2/proposals/0327-actor-initializers.md#delegating-initializers) for their delegating initializers, which is continued in the Sendability section.

Yippee!

Doug

---

<div class="post-metadata">

**Author:** ![kavon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kavon/32/9774_2.png) [@kavon](https://forums.swift.org/u/kavon)\
**Post date:** [December 14, 2021, 11:07pm UTC](https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972/3 "2021-12-14T23:07:42Z")

</div>

> [@Douglas\_Gregor](#):
>
> How bad would it be if we said that the default values of stored properties in actors have to be evaluated as if in a `nonisolated` context? And the same for default values of stored properties in global-actor-constrained structs/classes/enums?

Yes I think this would work just fine. We should say that the `nonisolated` context applies to all stored properties of a type that can be isolated to an actor. Having their default-value expressions be an actor-isolated context causes more trouble than it's probably worth. My attempt to preserve the power of syntactic sugar would make the language more complex. Programmers can always opt to do the initialization in an `init` with the appropriate actor-isolation if `nonisolated` doesn't work for them.

For global or static stored properties, it's not syntactic sugar, so I think its initializing expression needs to remain actor isolated to support something like this:

```swift
@MainActor
var x = 20

@MainActor 
var y = x + 2

```

---

<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:** [December 15, 2021, 12:30am UTC](https://forums.swift.org/t/pitch-2-on-actors-and-initialization/53972/4 "2021-12-15T00:30:31Z")

</div>

> [@kavon](#):
>
> Yes I think this would work just fine. We should say that the `nonisolated` context applies to all stored properties of a type that can be isolated to an actor. Having their default-value expressions be an actor-isolated context causes more trouble than it's probably worth. My attempt to preserve the power of syntactic sugar would make the language more complex. Programmers can always opt to do the initialization in an `init` with the appropriate actor-isolation if `nonisolated` doesn't work for them.

Agreed!

> [@kavon](#):
>
> For global or static stored properties, it's not syntactic sugar, so I think its initializing expression needs to remain actor isolated

Yes, I agree.

Doug
