-
Notifications
You must be signed in to change notification settings - Fork 198
Add discussion of global actor isolation inference. [SE-0316] #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
5e5349e
0f1df57
bee33b2
bfd27fd
6841627
0d7cf4a
481f72e
223091d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1392,6 +1392,118 @@ You can define your own singleton global actors | |||||
| using the `@globalActor` attribute, | ||||||
| as described in <doc:Attributes#globalActor>. | ||||||
|
|
||||||
| ### Isolation inference | ||||||
|
|
||||||
| Global actor isolation can be inferred in the code you write. Isolation | ||||||
| is inferred from class inheritance, protocol conformances, and context | ||||||
| where code is written. | ||||||
heckj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| #### Classes | ||||||
|
|
||||||
| If a superclass is isolated to a global actor, the global actor is | ||||||
| inferred on subclasses. For example, the code below has a main-actor | ||||||
amartini51 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| isolated class `Vehicle`, and a subclass `Train` that inherits | ||||||
| main-actor isolation: | ||||||
|
|
||||||
| ```swift | ||||||
| @MainActor | ||||||
| class Vehicle { | ||||||
| var currentSpeed = 0.0 | ||||||
| func makeNoise() { | ||||||
| // do nothing - an arbitrary vehicle doesn't necessarily make a noise | ||||||
|
||||||
| // do nothing - an arbitrary vehicle doesn't necessarily make a noise | |
| // Do nothing: an arbitrary vehicle doesn't necessarily make a noise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this comment mean? "An arbitrary vehicle doesn't necessary make a noise"? What makes it "arbitrary"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
The Vehicle class is a sort of abstract or placeholder superclass — it doesn't implement much behavior, but its subclasses like Train do. In real Swift code, abstract base classes like this are somewhat rare (unlike some other languages) because protocols or generics are often better tools. However, it does make a pretty approachable teaching example.
For additional context, this code listing follows the running code listing example from the Inheritance chapter earlier in the book. So we might also want to cross-apply edits there.
Also, if there's an editorial preference for colons rather than dashes, I can spin up a tracking bug for that. (Thinking of this because I have a related fix to normalize "OK" vs "Ok" in comments, per Apple Style, on a different branch that includes similar punctuation.)
amartini51 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| // do nothing - an arbitrary vehicle doesn't necessarily make a noise | |
| // Do nothing: an arbitrary vehicle doesn't necessarily make a noise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I'm not clear what this comment means.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
heckj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first example says “Vehicle and all of its methods and properties are isolated to the main actor”. This one says “all of its requirements”. After reading the next example, I think (?) that all of Switch’s methods and properties are also isolated to the main actor? Is that right? If so I find the first wording very helpful and clearer and I think it would help to use it here as well. If not, perhaps that fact could be made clearer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the language "methods and properties" when talking about concrete types and "requirements" when talking about protocols. But you're right that I should state that when @MainActor is inferred on Switch, all of its methods and properties become isolated to the main actor too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, yeah I think that’s really helpful because it’s changing more than just the requirements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed in bee33b2:
In the example above, the
Togglableprotocol is marked@MainActorto indicate that all of its requirements are isolated to the main actor. Swift infers main-actor isolation on types that conform toTogglable, so all methods and properties ofSwitchare isolated to the main actor, including theisOnproperty and thetogglemethod.
heckj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
heckj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
amartini51 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this example the function toggle is isolated to the main actor but isOn is not isolated to the main actor, is it? And if that’s the case, isn’t it not allowed to use it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this example, isOn is nonisolated, which is fine to access from a main actor isolated method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, is that because you can only access the toggle function if your Switch is currently living in the main actor and thus any access to isOn would necessarily be on the main actor as well? I always forget about that. Very cool.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasongregori Do you think it's worth calling out in the explanation of this code listing that isOn is nonisolated? Or ok to make this conversation thread as resolved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The paragraph under "Global Actors" needs some work:
Global Actors
"The main actor is a global singleton instance of the [
MainActor][] type.Normally, an actor can have multiple instances,
each of which provide independent isolation.
The possibility of multiple instances is why you declare all of an actor's isolated data
as instance properties of that actor.
However, because
MainActoris singleton ---there is only ever a single instance of this type ---
the type alone is sufficient to identify the actor,
allowing you to mark main-actor isolation using just an attribute.
This approach gives you more flexibility to organize your code
in the way that works best for you."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. Did you want to revisit this paragraph more together, or does the edit above fix the issues?