Default Values for Arguments in Nested Closures

Is there a reason why one cannot have a default value for a closure argument:

func bar(_ closure: @escaping (_ a: @escaping (_ b: Int, _ c: Int = 7) -> Void) -> Void) {  // Error: Default argument not permitted in a tuple type
    //...
}

Moreover, it seems sort of weird to me that for nested closures, I can't use any argument labels like the following:

func foo(_ closure: @escaping ((a: String) -> Void) -> Void) { }  // Error: Function types cannot have argument labels; use '_' before 'a'

foo { f in
    f(a: "Hello, World!")  // Error: Extraneous argument label 'a:' in call
}

The lack of these sorts of things makes it harder to use closures on the same level that I use functions.

At first I was thinking these don't exist because maybe these types of things would conflict with the type signature of the closure itself, but it doesn't for actual functions, so I can't see why it would for closures.

func baz(a: Int = 5) { }
print(type(of: baz))
// Prints "(Int) -> ()"

Would something like default arguments and/or argument labels for closure parameters be possible to add in Swift? Or is there some fundamental underlying reason why they are not available?

1 Like

This post might have your questions answered.

2 Likes