-
Notifications
You must be signed in to change notification settings - Fork 181
Add discussion of ~Protocol syntax [SE-0390] #351
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
Re-applied the changes from commit 1b663f7 (Fix invalid HTML comments (swiftlang#334), 2024-12-04) -- which landed on 'main' after this branch diverged -- to the new location for that content. Reviewed this branch's changes, before and after this merge, as follows: opendiff =(git diff main...b6439e7) =(git diff main...HEAD) References: 1b663f7 References: b6439e7
This PR is only part of the updates for SE-0390; tracking docs for syntax like |
TSPL.docc/LanguageGuide/Protocols.md
Outdated
that don't have any required methods or properties: | ||
|
||
- [`Copyable`][] for values that can be copied. | ||
- [`Sendable`][] for values that can be shared across concurrency contexts. |
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.
s/concurrency contexts/isolation domains/
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 to "concurrency domains" to match what TSPL currently uses in the Concurrency chapter. We can revisit as part of a pass Holly, Doug, and I want to make over some concurrency terminology.
TSPL.docc/LanguageGuide/Protocols.md
Outdated
|
||
- [`Copyable`][] for values that can be copied. | ||
- [`Sendable`][] for values that can be shared across concurrency contexts. | ||
- [`BitwiseCopyable`][] for values that con be copied, bit-by-bit. |
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.
- [`BitwiseCopyable`][] for values that con be copied, bit-by-bit. | |
- [`BitwiseCopyable`][] for values that can be copied, bit-by-bit. |
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
TSPL.docc/LanguageGuide/Protocols.md
Outdated
The Swift standard library defines several protocols | ||
that don't have any required methods or properties: | ||
|
||
- [`Copyable`][] for values that can be copied. |
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've always assumed that the Reference Manual is intended to house the more advanced material and that these dedicated sections are supposed to disclose the fundamentals more progressively.
Here, though, "can be copied" is written without explanation while the Reference Manual actually gives a super nice introduction about what that means which could be understood by even a beginner:
By default, Swift uses a set of rules
to automatically manage object lifetime across function calls,
copying values when required.
I wonder if it would be actually appropriate to have (very short) subsections here for each of these protocols to give just a sentence or two like the above and a simple example. A sentence like what I just quoted can be written with a much more approachable voice than what we would want to put (and have put) directly in the protocol documentation.
Other sections (for example, the "Generics" one above) could then refer to this for explanation of Copyable
and friends.
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're correct that the reference is more advanced, and the guide is more approachable. Although in this case, the portion you quoted from the reference is also a little more "tutorial" than usual, because there isn't any discussion of borrowing/consuming in the guide. (Someday maybe there should be though.)
I've expanded this discussion a little, moved Sendable
first since people are more likely to encounter it, and I added cross references to later sections that discuss the corresponding concepts. Adding too much here, this early in the book, also risks confusion — most developers can probably skip this section on a first read through TSPL until they actually need to work with (non)sendable or (non)copyable types. That said, there isn't an Advanced Protocols chapter later on, so this is still the right place for the concept of no-requirement protocols and the tilde syntax.
If the issue is that the API references for these protocols are too hard to understand, maybe we need to take some time to improve them. Copyable
could probably use a better introduction to what copying means and why you might care, and BitwiseCopyable
doesn't even have an API reference (tracked as rdar://128232700).
TSPL.docc/LanguageGuide/Protocols.md
Outdated
Swift automatically infers the conformance | ||
when you define a type that implements the protocol's requirements: | ||
|
||
- `Codable` |
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 was a little surprised to see this here. After checking Swift Evolution I think this implicit conformance only happens for distributed actor, not other struct/class/etc.? If that's the case, some clarification might be beneficial
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 calling this out. I re-reviewed the proposals that discuss Codable
and either distributed actors or implicit conformance (SE-0302, SE-0336, SE-0344, SE-0413, SE-0428), and I'm pretty sure you're correct. In particular, SE-0302 calls out that Codable
, Hashable
, and Equatable
all require explicit conformance, even when the compiler synthesizes the implementation of that conformance.
Moved this fact to a note box at the end of the section — although I'd also consider having it live just in the API reference for Codable
and not in TSPL, until we add a discussion of distributed actors that could mention it. (See also PR #326 with an in-progress draft of a Distributed Actors chapter.)
You can still write the conformance explicitly, | ||
but it doesn't have any effect. | ||
To suppress an implicit conformance, | ||
write a tilde (`~`) before the protocol name in the conformance list: |
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.
... especially since I tried to suppress Codable conformance, but the compiler complains about Type 'Codable' (aka 'Decodable & Encodable') cannot be suppressed
Co-authored-by: Apollo Zhu <[email protected]> Co-authored-by: Slava Pestov <[email protected]> Co-authored-by: Xiaodi Wu <[email protected]>
TSPL.docc/LanguageGuide/Protocols.md
Outdated
that don't have any required methods or properties: | ||
|
||
- [`Copyable`][] for values that can be copied. | ||
- [`Sendable`][] for values that can be shared across concurrency contexts. |
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 to "concurrency domains" to match what TSPL currently uses in the Concurrency chapter. We can revisit as part of a pass Holly, Doug, and I want to make over some concurrency terminology.
TSPL.docc/LanguageGuide/Protocols.md
Outdated
|
||
- [`Copyable`][] for values that can be copied. | ||
- [`Sendable`][] for values that can be shared across concurrency contexts. | ||
- [`BitwiseCopyable`][] for values that con be copied, bit-by-bit. |
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
TSPL.docc/LanguageGuide/Protocols.md
Outdated
Swift automatically infers the conformance | ||
when you define a type that implements the protocol's requirements: | ||
|
||
- `Codable` |
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 calling this out. I re-reviewed the proposals that discuss Codable
and either distributed actors or implicit conformance (SE-0302, SE-0336, SE-0344, SE-0413, SE-0428), and I'm pretty sure you're correct. In particular, SE-0302 calls out that Codable
, Hashable
, and Equatable
all require explicit conformance, even when the compiler synthesizes the implementation of that conformance.
Moved this fact to a note box at the end of the section — although I'd also consider having it live just in the API reference for Codable
and not in TSPL, until we add a discussion of distributed actors that could mention it. (See also PR #326 with an in-progress draft of a Distributed Actors chapter.)
TSPL.docc/LanguageGuide/Protocols.md
Outdated
The Swift standard library defines several protocols | ||
that don't have any required methods or properties: | ||
|
||
- [`Copyable`][] for values that can be copied. |
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're correct that the reference is more advanced, and the guide is more approachable. Although in this case, the portion you quoted from the reference is also a little more "tutorial" than usual, because there isn't any discussion of borrowing/consuming in the guide. (Someday maybe there should be though.)
I've expanded this discussion a little, moved Sendable
first since people are more likely to encounter it, and I added cross references to later sections that discuss the corresponding concepts. Adding too much here, this early in the book, also risks confusion — most developers can probably skip this section on a first read through TSPL until they actually need to work with (non)sendable or (non)copyable types. That said, there isn't an Advanced Protocols chapter later on, so this is still the right place for the concept of no-requirement protocols and the tilde syntax.
If the issue is that the API references for these protocols are too hard to understand, maybe we need to take some time to improve them. Copyable
could probably use a better introduction to what copying means and why you might care, and BitwiseCopyable
doesn't even have an API reference (tracked as rdar://128232700).
The example above is based on a Swift System API. | ||
https://github.com/apple/swift-system/blob/main/Sources/System/FileDescriptor.swift | ||
|
||
See also this PR that adds Sendable conformance to FileDescriptor: | ||
https://github.com/apple/swift-system/pull/112 | ||
|
||
XXX SE-0390 uses the same example but ~Copyable -- is that better? |
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.
Reviewers: Any opinion on this alternative version of the code example?
@@ -752,11 +752,14 @@ a nonfailable initializer or an implicitly unwrapped failable initializer. | |||
``` | |||
--> | |||
|
|||
## Protocols That Don't Have Requirements | |||
## Protocols That Have Semantic 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.
All protocols have semantic requirements (we have said repeatedly that "protocols are not just bags of syntax"). Perhaps:
## Protocols That Have Semantic Requirements | |
## Protocols That Have Only Semantic Requirements |
You can also use a protocol to mark types | ||
that satisfy some *semantic* requirements --- | ||
requirements about how values of those types behave | ||
and about operations that they support --- | ||
not just requirements that you express in code. |
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.
not just requirements that you express in code. | |
not just requirements that you express in code. |
Same comment regarding language here.
Fixes: rdar://110848216