Skip to content
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

Closed
swift-ci opened this issue Dec 3, 2018 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself documentation

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 3, 2018

Previous ID SR-9403
Radar None
Original Reporter swiftHoney (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Documentation
Assignee None
Priority Medium

md5: dad8108ba7be2dbb129c570da8e99654

Issue Description:

Looking at:

/// Every protocol has an implicitly-created associated type 'Self' that

The comment says:

/// Every protocol has an implicitly-created associated type ‘Self’ that
/// describes a type that conforms to the protocol

created has the connotation that it always has the type attached

Yet the following code compiles without issues:

protocol Something {
}
var NoSelfOrAssociatedtypeRequirementArray : [Something] = []

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.

/// Every protocol that uses Self in its requirements will have an implicitly-created associated type ‘Self’ that
/// describes a type that conforms to the protocol
@belkadan
Copy link
Contributor

belkadan commented Dec 4, 2018

cc @slavapestov, @DougGregor

@slavapestov
Copy link
Member

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.

@slavapestov
Copy link
Member

#21018

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 5, 2018

Comment by mohammad faani (JIRA)

Thank you for your swift action
@slavapestov

Me and a senior a dev are a bit confused by your new comment. Protocols can't have generic parameters.
Do you mean something like this is happening under the hood:

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
Can you explain it a bit more?

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 {}

@slavapestov
Copy link
Member

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'.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 5, 2018

Comment by mohammad faani (JIRA)

hmmm.
we mere mortals and you Asgardians!

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.

@slavapestov
Copy link
Member

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).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@mfaani
Copy link

mfaani commented Jun 9, 2022

@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 :)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself documentation
Projects
None yet
Development

No branches or pull requests

4 participants