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

'rethrows' function with non-throwing default requires 'try'

    XMLWordPrintable

    Details

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

      swift/master

      Description

      If I write a rethrowing function that takes a possibly throwing closure and provide a non-throwing default value, I have to use 'try' when calling that function without an argument. Is this the right behavior? The compiler should be aware that the default closure doesn't throw, so it would be nice if the "default" function call wasn't marked as rethrowing:

      func foo(_ f: () throws -> Void = { print("Hello!") }) rethrows {
          try f()
      }
      
      foo({ print("Hey!") })
      // Hey!
      foo()
      // error: call can throw but is not marked with 'try'
      // note: call is to 'rethrows' function, but argument function can throw
      try foo()
      // Hello!
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nnnnnnnn Nate Cook
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: