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

Optional closure type is always considered @escaping

    XMLWordPrintable

    Details

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

      OSX 10.11.6 (15G31)
      MacBook Pro (Retina, Mid 2012)
      Version 8.0 beta 5 (8S193k)
      Toolchain: Swift Development Snapshot 2016-08-07 (a)

      Description

      The following code snippet doesn't work anymore in the current Swift 3 snapshot:

      func takes(anOptionalClosure: (() -> Void)?) {
          if let aClosure = anOptionalClosure {
              aClosure()
          }
      }
      
      func takes(aClosure: () -> Void) {
          takes(anOptionalClosure: aClosure)
      }
      

      > main.swift:18:30: Cannot convert value of type '() -> Void' to expected argument type '(() -> Void)?'

      My guess is that the type checker thinks that `(() -> Void)?` is interpreted as a tuple type. But the parens are only necessary to make the closure optional. Interestingly this only happens with function types. `(String)?` e.g. type checks correctly.

      The following snippet works as expected:

      func takes(anOptionalClosure: (() -> Void)?) {
          if let aClosure = anOptionalClosure {
              aClosure()
          }
      }
      
      // Hint: Added two surrounding parens to the argument type
      func takes(aClosure: (() -> Void)) {
          takes(anOptionalClosure: aClosure)
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              milseman Michael Ilseman
              Reporter:
              ilijatovilo Ilija Tovilo
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: