-
Notifications
You must be signed in to change notification settings - Fork 198
Add a section on isolated conformances to the guide [SE-0470] #381
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?
Conversation
This change adds a section on global-actor isolated protocol conformances to the Concurrency chapter of the language guide.
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.
Thank you for writing this!
Use semantic line breaks for prose, title case for headings, four spaces for Swift code indentation, and contractions.
Replace a few uses of "called" with "used" when talking about using protocol requirements. Also removed the subheadings in the Using Isolated Conformances section.
Isolated conformance inference is gated behing the InferIsolatedConformances upcoming feature. Add a dedicated section for inference and a note about the upcoming feature identifier per the guidance in Style.md.
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.
Looking good! Had some clarification ideas you may want to take, commented inline
88db53c
to
d657461
Compare
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 for writing all of this! I've marked a bunch of specific places throughout the diff — and some general comments below.
Let's expand the transitions and explanations. Some sections have a main point too far into the section, and we can elevate that to the introductory paragraph. Several of the code listings would benefit from a paragraph after them that takes the reader through what the code's doing in more detail.
Some subsections don't need as many headings, and headings don't need to be nested as deeply. (Although there's no issue with short sections when they're called for.) Let's try to fit more with the level and length of headings in the existing book. Some of the choices around sectioning in the existing content are based on what DocC handles well, some are based on editorial feedback, and some are just convention that we can vary. Here's an outline of this chapter:
- Defining and Calling Asynchronous Functions
- Asynchronous Sequences
- Calling Asynchronous Functions in Parallel
- Tasks and Task Groups
- Task Cancellation
- Unstructured Concurrency
- Isolation
- The Main Actor
- Actors
- Global Actors
- Sendable Types
- Isolated Protocol Conformances <——— NEW
- Declaring an Isolated Conformance
- Inferring an Isolated Conformance
- Data-Race Safety for Isolated Conformances
- Using Isolated Conformances
- Restricting Isolated Conformances in Concurrent Code
- Protocols That Require
Sendable
orSendableMetatype
- Protocols That Require
Prefer active voice and present tense over passive voice and future tense. We'll catch this during the editorial pass too.
the conformance is only used on the actor | ||
that the conformance is isolated to. | ||
|
||
### Declaring an Isolated Conformance |
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.
You probably don't need a heading here after just one paragraph.
|
||
## Isolated Protocol Conformances | ||
|
||
Protocols that are nonisolated |
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 either define "nonisolated protocol" if it's a new concept being introduced, or find a way to refer to normal protocol conformance that doesn't sound like a new term.
while ensuring that state is only accessed | ||
from within the actor. | ||
|
||
### Inferring an Isolated Conformance |
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 developer reading TSPL isn't doing the inference, the compiler is. Maybe "Inferred" or "Using Inferred" in the heading?
This allows the implementation of the conformance | ||
to use global actor isolated state | ||
while ensuring that state is only accessed | ||
from within the 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 walk through the code listing more step-by-step in its explanation paragraph. For example:
- Why is
Person
marked@MainActor
? - Call out that writing
@MainActor Equatable
is how you mark the conformance as actor-isolated
Expand "This allows" so the reader doesn't have to guess what "this" refers to. Here, probably "This isolated conformance allows"?
} | ||
|
||
extension Person: @MainActor Equatable { | ||
static func ==(lhs: Person, rhs: Person) -> Bool { |
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.
To match TSPL style elsewhere:
static func ==(lhs: Person, rhs: Person) -> Bool { | |
static func == (lhs: Person, rhs: Person) -> Bool { |
Likewise below; marking it just here.
In generic code, | ||
a conformance requirement to `Sendable` or `SendableMetatype` | ||
tells Swift that an instance or metatype value is safe to use concurrently. |
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.
This reads like a definition of Sendable
but I think it's about the where D: Sendable
clause in the code below? We can check terminology here too — I see "conformance requirement" only twice in TSPL, and in both places it refers to the requirements that a type must implement in order to conform to a protocol.
The above code would admit data races | ||
if the conformance to `Dancer` was isolated, | ||
because the implementation of `perform` | ||
may access global actor isolated state. |
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'm having a hard time with this sentence, probably because of the irrealis mood on the verbs (would admit, was isolated, may access). Are you saying that the code above is safe, and then describing why by telling me about something that can't happen? Or are you describing a scenario where the code has possible data races?
func perform() | ||
} | ||
|
||
func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where T: SendableMetatype { |
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 think the T
is just a leftover from a previous version that had different type names?
func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where T: SendableMetatype { | |
func performConcurrently<D: Dancer>(n: Int, for: D.Type) async where D: SendableMetatype { |
Note that `Sendable` requires `SendableMetatype`, | ||
so an explicit conformance to `SendableMetatype` is only necessary | ||
if the type is non-`Sendable`. |
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.
This information seems like it should come earlier.
let items = performConcurrently(n: 10, for: Ballerina.self) // Error | ||
``` | ||
|
||
##### Protocols That Require `Sendable` or `SendableMetatype` |
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.
We can't use H5 or code voice in headings.
This change introduces a section on global-actor isolated protocol conformances, which were introduced in SE-0470.
Fixes: rdar://148361008