Learn

Because closures are so common in Swift, there are quite a few niceties or “syntactic sugar” that are available to shorten your code. To explain these, let’s take a look at a function that takes a closure as an argument:

func performOperation(on valueOne: Int, and valueTwo: Int, using operation: (Int, Int) -> Int) -> Int { operation(valueOne, valueTwo) }

Let’s break down how we can shorten our code with Swift closure syntax when calling the performOperation(int1:int2:_) above. Let’s start with a fully explicit call:

performOperation(on: 1, and: 3, using: { (valueOne: Int, valueTwo: Int) -> Int in return valueOne + valueTwo })

Here we pass an inline closure to the operation input. We explicitly define the parameter types (valueOne: Int, valueTwo: Int) and return type Int. This is all well and good and perfectly valid code. But, it can be shortened quite a bit. If you recall from the last exercise, we could use trailing closure syntax to shorten the calling code here.

performOperation(on: 1, and: 3) { (valueOne: Int, valueTwo: Int) -> Int in return valueOne + valueTwo }

Now, let’s dive into the other syntax shortening niceties that Swift provides:

Type inference: Swift is a strongly typed language meaning it can infer the types based on the function definition. Above, we don’t need to be explicit with the parameter types and return types:

performOperation(on: 1, and: 3) { valueOne, valueTwo in return valueOne + valueTwo }

Implicit returns: If a closure has a single expression, the return keyword can be omitted. Swift can infer the return type based on the function definition:

performOperation(on: 1, and: 3) { valueOne, valueTwo in valueOne + valueTwo }

Shorthand argument names: argument names can be omitted in favor of $0, $1, and so on, corresponding to each argument in the closure. Although this does shorten your calling code, it can sacrifice readability so use with caution:

performOperation(on: 1, and: 3) { $0 + $1 }

And lastly, operators: Swift’s Int type has an Int-specific definition of the + operation that matches the type of the closure. This mean we can pass a single operator as to the closure:

performOperation(on: 1, and: 3, using: +)

Notice we need the using argument label here. This is because + is a static function that adds two numbers together. Up until this point, we have been passing anonymous closures which are defined inline with the function call.

Pretty neat, eh? This is quite readable and can be reused with other operators like -, \ and *.

Instructions

1.

Call the transform function with the provided array and a closure that reverses each string. Be as explicit as possible with the type definitions.

NOTE: The reversed() function on String doesn’t return a String so we need to wrap the result in a String to convert it:

String(str.reversed())

2.

Next, call the function without the return keyword and exclude the return type. Print the result to the console.

3.

Finally, call the function using shorthand argument names.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?