Uploaded image for project: 'Swift'
  1. Swift
  2. SR-6612

Contextual closure type expects N arguments, but <N was used in closure body.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Duplicate
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Swift 4 via Xcode 9.2

      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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              davidbjames David James
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: