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

Swift compiler requires closures without parameter lists to reference all arguments

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Swift 2.2

      Description

      Swift closures that do not explicitly declare an internal parameter list must reference all arguments using implicit $n shorthand names. If they do not, Swift complains that the contextual type for the closure argument "expects n arguments, which cannot be implicitly ignored." This requirement diminishes the efficacy of Swift's $n syntactic sugar. Eliminating the requirement means:

      • {} becomes a valid 'noop' closure in any context requiring a Void-returning closure.
      • Implementations can discard unnecessary code cruft and streamline their minimum implementation from { _(, _)* in }

        to {}.

      • { expression }

        becomes a valid closure in any context requiring a return value. The expression can offer a simple expression or literal, such as

        { 42 }

        .

      • The closure can mention some of its parameters without having to mention all of its parameters.

      All the following closures should be valid and compile without error or warning:

      {{let _: () -> Void = {}
      let _: (Int) -> Void = {}
      let _: (Int, Int) -> Int =

      { 5 }

      let _: (Int, Int) -> Int =

      { $0 }

      let _: (Int, Int) -> Int =

      { $1 }

      doThing(withCompletion: {})
      let x: (T) -> Void = {}}}

      Chris L writes: "I consider this to be an obvious bug in the compiler. I don’t think it requires a proposal. Unfortunately it is non-trivial to fix…"

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                erica erica sadun
              • Votes:
                2 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated: