Can swift closures be set to a default value when used as a parameter in a function?

Eric

A pretty handy feature of Swift functions is that function parameters can have default values:

func someFunction(parameterWithDefault: Int = 42) {
    //if no arguments are passed to the function call,
    //value of parameterWithDefault is 42
}

If a parameter is a closure, is there a way to make it have a default value? See the example below:

func sendBody(
    body: NSData? = nil,
    success: (data: NSData) -> Void,
    failure: (data: NSData?) -> Void) {
}

Is there a way to not force the developer to pass a value for success or failure when calling sendBody?

Airspeed Velocity

Yes, functions are just values you so you can supply them as defaults:

// just to show you can do it with inline closures or regular functions
func doNothing<T>(t: T) -> Void { }

func sendBody(
    body: NSData? = nil,
    success: (data: NSData) -> Void = { _ in return },
    failure: (data: NSData?) -> Void = doNothing
)
{  }

Alternatively, you could make them optional, that way you can detect if the caller passed one:

func sendBody(
    body: NSData? = nil,
    success: ((NSData) -> Void)? = nil,
    failure: ((NSData?) -> Void)? = nil
    )
{ success?(NSData()) }

sendBody(success: { _ in print("ah, yeah!") })

Also worth noting if you’re doing this: if the caller uses the trailing closure syntax, this will be the last closure in the argument list. So you want the last one to be the one the user is most likely to want to supply, which is probably the success closure:

func sendBody(
    body: NSData? = nil,
    success: ((NSData) -> Void)? = nil,
    failure: ((NSData?) -> Void)? = nil
    )
{
    if success != nil { print("passed a success closure") }
    if failure != nil { print("passed a failure closure") }
}

// this prints "passed a failure closure"
sendBody { data in
    print("which closure is this?")
}

Other than this, the order in the function declaration doesn’t matter to the caller – defaulted arguments can be supplied in any order.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set default value to C array when used as function argument

Swift seperate parameter without a comma when using closures as a function parameter

Python: How to set the default value when the parameter type is function?

Can I set default value to the function when passing it to a Child component?

Default value for optional generic parameter in Swift function

Set the default value for the key of the function object parameter

Set a default parameter value for a JavaScript function

ES6 destructuring object, default value on assignment when not used as a function parameter?

Sending default parameter as null and use default value set in function

How can I set default parameter value in Vue.js method function?

What is the cleanest way in swift to use an optional in a function parameter when nil should return a specific default value?

Django factoryboy fail when used as default parameter value

How to set value for specific default value of a parameter in a javascript function

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Default value for function as parameter

MySQL UUID function produces the same value when used as a function parameter

Swift: using member constant as default value for function parameter

How to set a default value for an optional positional parameter of type Function?

Set default value for parameter of List<string> type in function

How can I set default value in parameter substitution as an array of elements?

Set function default this value

How can I programmatically determine the default value of a function parameter?

Default optional parameter in Swift function

Dart set default value for parameter

Set default value and type for parameter

Why can't GETDATE() be used as the default value of a procedure parameter or a value in an EXECUTE statement?

get function parameter with default value

JavaScript function default parameter value

Is It Possible To Set Default Parameter Value On A Rest Parameter