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-6612] Contextual closure type expects N arguments, but <N was used in closure body. #49161

Closed
davidbjames opened this issue Dec 14, 2017 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@davidbjames
Copy link

Previous ID SR-6612
Radar None
Original Reporter @davidbjames
Type Bug
Status Resolved
Resolution Duplicate
Environment

Swift 4 via Xcode 9.2

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

md5: f98cf2e06ba3ef8b4259e0417cd0c208

duplicates:

  • SR-1528 Swift compiler requires closures without parameter lists to reference all arguments

relates to:

  • SR-586 $0 captures all closure argument as single tuple when no larger-numbed $N used

Issue Description:

Closures with arguments should allow using only some or none of those arguments. However, the current state of things requires using at least the final argument, else error.

Example:

func foo(_ closure:(Int,Int,Int)->Void) {
    closure(1,2,3)
}

foo { } // ERROR: "expects 3 arguments, which cannot be implicitly ignored"
foo { print($0) } // ERROR: "expects 3 arguments, but 1 was used"
foo { print($1) } // ERROR: "expects 3 arguments, but 2 were used" Huh? There was only 1 used.
foo { print($0,$1) } // ERROR: "expects 3 arguments, but 2 were used"
foo { print($0,$1,$2) } // OK
foo { print($2) } // OK
// ^^^ Though it might as well say "expects 3 arguments, but 1 was used"

I am suggesting that all of the above scenarios should work without error.

Here is a more practical example, where a second argument is an index which may or may not be used (since it's optional it is intentionally the last arg):

func each(_ closure: (T, Int) -> Void) { .. }
// should be able to call using first parameter only:
foo.each { $0.doSomething } // ERROR: "expects 2 arguments, but 1 was used"

See also: https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160118/007095.html

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

No branches or pull requests

1 participant