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-12388] Cannot Infer Generic Type in Function with Nested Closure Parameter #54825

Closed
swift-ci opened this issue Mar 20, 2020 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12388
Radar rdar://problem/60832833
Original Reporter Wildchild9 (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

Tested on Xcode Version 11.3.1 (11C504) and Xcode Version 11.4 beta 3 (11N132i).

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 6fe608cbd121adcea05965fde2b18fc8

Issue Description:

Let's say I have the following function:

func foo<T>(_ closure: ((T) -> Void) -> Void) {
    print(type(of: T.self)) 
}

Calling the following infers the proper type for T, String:

foo { bar in
    bar("Hello")
}
// Prints "String.Type"

The problem arises when bar is called an additional time within the closure:

foo { bar in  // Error: Generic parameter 'T' could not be inferred
    bar("Hello")
    bar("World")
}

The only way this issue can be worked around, is by explicitly specifying the type of `bar`:

foo { (bar: (String) -> Void) in
    bar("Hello")
    bar("World")
}
// Prints "String.Type"

It seems to me that if Swift can infer the type for the single invocation case, it ought to be able to infer the type for the case with multiple invocations.

@beccadax
Copy link
Contributor

@swift-ci create

@xedin
Copy link
Member

xedin commented Mar 26, 2020

Wildchild9 (JIRA User) Unfortunately this is just a nature of how multi-expression closures are type-checked. The body of the closure is not type-checked together with a call to `foo` so in order to resolve any generic parameters either only parameters or whole type has to be specified explicitly.

@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.
Projects
None yet
Development

No branches or pull requests

3 participants