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
[SR-9403] The 'Every protocol has an implicitly-created associated type ‘Self’' comment is misleading #51869
Comments
It's not actually an associated type at all (maybe it was at some point?) I'll remove the comment and add a different one in ProtocolDecl. |
Comment by mohammad faani (JIRA) Thank you for your swift action Me and a senior a dev are a bit confused by your new comment. Protocols can't have generic parameters. protocol P<Self> {
// not sure what property name I should put here maybe _self
var _self : Self
} error: protocols do not allow generic parameters; use associated types instead I'm also wondering if there is any significance in your example: protocol Cloneable {
func clone() -> Self
} Could you have just wrote the following as your example too? protocol S {} |
Yes, the 'Self' generic parameter is created under the hood. Protocols cannot have user-defined generic parameters. The Cloneable example is there just to show a usage of 'Self'. |
Comment by mohammad faani (JIRA) hmmm. So does this also have the 'implicitly-created 'Self' generic parameter'? protocol S {} I guess it doesn't right? Or that it does, but since there isn't any usage of `Self` then no requirement is created at the protocol level. |
All protocols have a 'Self' parameter. The check for being able to use a protocol as a type looks as the signature of each method requirement to see if Self appears in a parameter position (note that Self in a return position, as in the Cloneable example, does not prevent you from using the protocol as a type). |
@slavapestov Was happy see half the WWDC 2022 - Design protocol interfaces in Swift talk is about this :D "We mere mortals and you Asgardians!" above, still applies. Your bug pin is 10X cooler than the ones we got at the conference :) |
Additional Detail from JIRA
md5: dad8108ba7be2dbb129c570da8e99654
Issue Description:
Looking at:
swift/include/swift/AST/Decl.h
Line 2589 in ebd701c
The comment says:
created has the connotation that it always has the type attached
Yet the following code compiles without issues:
From what I understand the compiler assigns `Self` as an associatedtype only if Self is used.
Hence I think the comment should be adjusted. e.g.
The text was updated successfully, but these errors were encountered: