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

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

    XMLWordPrintable

    Details

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

      Swift 2.1

      Description

      The following code does not compile, but should:

      func foo(val: Int) { }
      
      func bar(closure: (Int,Int) -> Void) {
          closure(0, 1)
      }
      
      bar { foo($0) }       // compiler error
      bar { foo($1) }       // just dandy
      bar { foo($0 + $1) }  // also works
      

      In the absence of $1, the implicit $0 variable captures all the arguments as a tuple. The compiler error is:

      Cannot convert value of type (Int, Int) to expected argument type Int

      John McCall indicated on swift-evolution that this is a bug, not an intentional feature:

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

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              pcantrell Paul Cantrell
              Votes:
              2 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: