-
Notifications
You must be signed in to change notification settings - Fork 169
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
Add more existentials documentation #340
base: main
Are you sure you want to change the base?
Conversation
print("The full name is \(fullyNamed.fullName)") | ||
} | ||
|
||
printFullName(ncc1701) |
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.
ncc1701
is defined on line 233
An existential can also creating by writing `any` before a protocol composition type (see <doc:Protocols#Protocol-Composition>). This creates a box which holds a structure, class or enum that conforms to all the protocols listed. | ||
|
||
``` | ||
protocol Named { |
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 protocol is also defined in line 1629 of the "Protocols" chapter
Thanks for writing this up as a pull request, and in particular thanks for taking the time to include the cross references between running code listings. I'll be out of the office next week for the Thanksgiving holiday, and will continue working with you on these changes when I get back. A few initial notes:
|
Thanks for the review!
Yes, I was trying to look up what the existentials any syntax supports (i.e. exactly what is legal to put in
Sure, I can revert this, but it seems strange to me that the Protocols chapter (which comes before the Generics chapter) talks about opaque types. |
@@ -1604,9 +1608,9 @@ that tries to adopt `SomeClassOnlyProtocol`. | |||
## Protocol Composition | |||
|
|||
It can be useful to require a type to conform to multiple protocols at the same time. | |||
You can combine multiple protocols into a single requirement | |||
You can combine multiple protocols into a single *protocol composition type* |
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 language reference calls this a "protocol composition type", but I think the compiler warnings call this a "protocol-constrained type" 🤔
@ldct Thanks for the revisions, and for your patience — it took longer than I expected to come back to this. I spent some time today look at this again and checking the difference between The bare syntax for existentials was originally going to produce a warning in Swift 6 mode, per SE-0335; that warning was deferred to a future language version. TSPL switched over to the The reason for the forward reference from the Protocols chapter to Generics is partly historical, and partly as a way to steer folks away from existentials. There used to be an longer discussion of existentials here, which was replaced in #103 as part of adding Do you have an example of a compiler warning that refers to “protocol-constrained type”? I don’t think we want to change the TSPL spelling here, but it might be worth working to improve the compiler message, to make it clearer. I think the next step here is to decide between showing the |
This PR contains a few changes to add more documentation around existentials. Note that the "Protocols" chapter comes before the "OpaqueTypes" chapter.
Protocols as Types
with other examples of using protocols as typesProtocol Composition Type
is the named used by the Language Reference.Protocol Composition
for this. In my mind, this is a short form.