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-899] .self can be omitted if a function has only one parameter #43511

Closed
swift-ci opened this issue Mar 8, 2016 · 4 comments
Closed

[SR-899] .self can be omitted if a function has only one parameter #43511

swift-ci opened this issue Mar 8, 2016 · 4 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 8, 2016

Previous ID SR-899
Radar None
Original Reporter tannernelson (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 01bb7b482c4c4fc90912856191c179ba

is duplicated by:

  • SR-1306 Inconsistent behavior with labeled type parameters

Issue Description:

I'm wondering what the reasoning is for allowing Types to be passed into a function without explicitly declaring .self if there is only one function parameter.

When ommitting .self:

  • The function has 1 parameter: compiles

  • The function has > 1 parameter: Expected member name or constructor call after type name

Take the following code:

func test<T: Any>(type: T.Type, two: String) {
    print(type)
}

func test<T: Any>(type: T.Type) {
    print(type)
}

test(Int.self) //no error
test(Int) //no error

test(Int.self, two: "") //no error
test(Int, two: "") //error: Expected member name or constructor call after type name

To give some background, I am trying to create a function with the signature

func get<T: Any>(path: String, _ star: T.Type, handler: (Request, T) throws -> Response)

That can be used like so

app.get("users", Int) { request, id in 
   print("You requested user with ID \(id)")
   ...
}

This get function works if you include .self after all of the Types, but that is not as clean and seems unnecessary unless I'm missing something.

@belkadan
Copy link
Contributor

belkadan commented Mar 8, 2016

It's a bug. .self is supposed to be required everywhere. Unfortunately, we've had the bug for a while, so I'm not sure how much code we'd break by changing it.

If we wanted to remove the requirement, that would be considered a language change and would have to go through the Swift Evolution Process.

@belkadan
Copy link
Contributor

Nate notes in the dup that having a label on the lone parameter also forces .self to be required.

@DougGregor
Copy link
Member

There is a Swift evolution proposal to remove this requirement: https://github.com/apple/swift-evolution/blob/master/proposals/0090-remove-dot-self.md

I actually have a fix in hand, but the current limbo state of SE-0090 makes it harder to justify pushing this fix. See the commit here:

4a60b6c

@DougGregor
Copy link
Member

Fixed by

2807a17

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants