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

Discarding a closure from a @discardableResult function results in a compile error.

    XMLWordPrintable

    Details

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

      Description

      A function marked with @discardableResult should not throw an error if it is returning closure is unused.

      The current work around is to set _ to the returning closure.

      @discardableResult func run (_ closure: @escaping ()->()) -> (()->()) {
        closure()
        return closure
      }
      
      var closure={print(1)}
      _ = run(closure)
      // Prints 1
      
      _ = run(run(closure))
      // Prints 1
      // Prints 1
      
      run(closure)
      // should Print 1 but errors with:
      //   discardableResultOfClosure.swift:14:1: error: expression resolves to an unused function
      //   run(closure)
      //   ^~~~~~~~~~~~
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              Amnykon MicahBunting
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: