Shorthand syntax

Compared to functions, closures are designed to be lightweight. There are many ways to shorten their syntax. First, if the closure consists of a single return statement, you can leave out the return keyword, like so:

multiplyClosure = { (a: Int, b: Int) -> Int in a * b

}

Next, you can use Swift’s type inference to shorten the syntax even more by removing the type information:

multiplyClosure = { (a, b) in a * b

}

Remember, you already declared multiplyClosure as a closure taking two Ints and returning an Int, so you can let Swift infer these types for you.

And finally, you can even omit the parameter list if you want. Swift lets you refer to each parameter by number, starting at zero, like so:

multiplyClosure = {

$0 * $1

}

The parameter list, return type and in keyword are all gone, and your new closure declaration is much shorter than the original. Numbered parameters like this should really only be used when the closure is short and sweet, like the one above. If the parameter list is much longer it can be confusing to remember what each numbered parameter refers to. In these cases you should use the named syntax.

Consider the following code:

func operateOnNumbers( a: Int, b: Int,

operation: (Int, Int) -> Int) -> Int { let result = operation(a, b)

print(result) return result

}

This declares a function named operateOnNumbers, which takes Int values as its first two parameters. The third parameter is named operation and is of a function type. operateOnNumbers itself returns an Int.

You can then use operateOnNumbers with a closure, like so:

let addClosure = { (a: Int, b: Int) in a + b

}

operateOnNumbers(4, 2, operation: addClosure)

Remember, closures are simply functions without names. So you shouldn’t be surprised to learn that you can also pass in a function as the third parameter of operateOnNumbers, like so:

func addFunction( a: Int, b: Int) -> Int { return a + b

}

operateOnNumbers(4, 2, operation: addFunction)

operateOnNumbers is called the same way, whether the operation is a function or a closure.

The power of the closure syntax comes in handy again: You can define the closure inline with the operateOnNumbers function call, like this:

operateOnNumbers(4, 2, operation: { (a: Int, b: Int) -> Int in return a + b

})

There’s no need to define the closure and assign it to a local variable or constant; you can simply declare the closure right where you pass it into the function as a parameter!

But recall that you can simplify the closure syntax to remove a lot of the boilerplate code. You can therefore reduce the above to the following:

operateOnNumbers(4, 2, operation: { $0 + $1 })

In fact, you can even go a step further. The + operator is just a function that takes two arguments and returns one result so you can write:

operateOnNumbers(4, 2, operation: +)

There’s one more way you can simplify the syntax, but it can only be done when the closure is the final parameter passed to a function. In this case, you can move the closure outside of the function call:

operateOnNumbers(4, 2) {

$0 + $1

}

This may look strange, but it’s just the same as the previous code snippet, except you’ve removed the operation label and pulled the braces outside of the function call parameter list. This is called trailing closure syntax.

results matching ""

    No results matching ""