-
Notifications
You must be signed in to change notification settings - Fork 204
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 6 commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1371,7 +1371,6 @@ if you try to add concurrent code to this function, | |||||
| introducing a possible suspension point, | ||||||
| you'll get compile-time error instead of introducing a bug. | ||||||
|
|
||||||
|
|
||||||
| ## Global Actors | ||||||
|
|
||||||
| The main actor is a global singleton instance of the [`MainActor`][] type. | ||||||
|
|
@@ -1392,6 +1391,146 @@ You can define your own singleton global actors | |||||
| using the `@globalActor` attribute, | ||||||
| as described in <doc:Attributes#globalActor>. | ||||||
|
|
||||||
| ## Isolation Inference | ||||||
|
|
||||||
| When you subclass an actor-isolated class | ||||||
| and conform to an actor-isolated protocol, | ||||||
| Swift infers the actor isolation for you. | ||||||
| These inference rules apply to the main actor and to other global actors. | ||||||
|
|
||||||
| For classes that are isolated to a global actor, | ||||||
| Swift infers that their subclasses have the same isolation. | ||||||
| For example, | ||||||
| the code below declares a main-actor isolated class `Vehicle`, | ||||||
| and a subclass `Train` that inherits from `Vehicle`: | ||||||
|
|
||||||
| ```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.)
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 `Vehicle` class is isolated to the main actor --- | |
| the `Vehicle` class isolates to the main actor --- |
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.
Let's discuss wording here.
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
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.
| Swift infers isolation from protocols | |
| Swift only infers isolation from protocols |
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.
My original placement of "only" is right next to the word that it modifies, to avoid ambiguity — do you think there's any risk of a reader misreading this? For example, to misread as "Swift infers isolation" but it doesn't do anything else. The original was based on editorial guidance I've gotten in the past, moving the "only" to the most specific location.
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.
Revert to "only when...". Sounds good to me!
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.
| only when you write the conformance as part of the type's declaration. | |
| when you write the conformance as part of the type's declaration. |
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
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.
| then isolation inference applies to | |
| then isolation inference only applies to |
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. Same reason for my original wording as above — although I think the risk of misreading is much lower here.
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.
| only requirements that are part of that the extension. | |
| requirements that are part of that extension. |
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
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?