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-10121] Support Self in a covariant position of a class method's parameter #52523

Open
hamishknight opened this issue Mar 16, 2019 · 4 comments
Assignees
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis

Comments

@hamishknight
Copy link
Collaborator

Previous ID SR-10121
Radar None
Original Reporter @hamishknight
Type Improvement
Status In Progress
Resolution
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement, TypeChecker
Assignee @theblixguy
Priority Medium

md5: e3cd47160754677e6ad2288fcf1531fa

Issue Description:

Now that SE-0068 is implemented, it would also be nice to support the use of Self in a covariant position of a class method's parameter, e.g:

class C {
  func foo(_ fn: (Self) -> Void) {}
}
@belkadan
Copy link
Contributor

That's not necessarily connected to SE-0068, since the Self you want here is a dynamic self. @jckarter, do you think this would have to go through evolution?

@hamishknight
Copy link
Collaborator Author

IMO it fits with the decision notes of SE-0068, which specifically calls out that Self within the body of a class should refer to the dynamic type:

This proposal had light discussion in the community review process, but the core team heavily debated it. It includes two pieces:

1. Expanding the existing support for Self to work in value types, and in the bodies of classes.

2. Replacing the x.dynamicType expression with x.Self, a purely syntactic change that eliminates the “dynamicType” keyword.

The core team has accepted the first half for this proposal. This allows the use of “Self” as shorthand for referring to the containing type (in the case of structs, enums, and final class) or the dynamic type (in the case of non-final classes). Most of the discussion in the core team centered around whether people familiar with the former behavior would be surprised by the (more general) behavior when using it in a class, but they came to agree that this is actually a simple and general model, and a helpful point of consistency.

@jckarter
Copy link
Member

I agree with Hamish's sentiment. There's no obvious reason to me this shouldn't work, if we can make the compiler handle it correctly.

@theblixguy
Copy link
Collaborator

I have created a PR: #23430

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants