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

Closure signature in Swift 3 required for inout params

    XMLWordPrintable

    Details

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

      Description

      For whatever reason, you have to declare the closure signature with inout variables in Swift 3. You didn't in Swift 2. Example below. Also affected the new stdlib sequence functions.

      Swift 2:

      func myFor<T>(args: T, _ test: (T) -> Bool, _ next: (inout T) -> Void, body: (inout T) -> Void) {
          var state = args
          while test(state) {
              defer { next(&state) } ; body(&state)
          }
      }
      
      myFor(0, { $0 < 10 }, { $0 += 2 }) {
          print($0)
      }
      

      Swift 3:

      func myFor<T>(_ args: T, _ test: (T) -> Bool, _ next: (inout T) -> Void, body: (inout T) -> Void) {
          var state = args
          while test(state) {
              defer { next(&state) } ; body(&state)
          }
      }
      
      myFor(0, { $0 < 10 }, { (i: inout Int) in  return i += 2 }) {
          print($0)
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              erica erica sadun
              Reporter:
              erica erica sadun
              Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: