Useful details about underscore keyword in Swift

Swift is known for its ability to help creating readable and self explanatory code. Such practice is efficient, because writing meaningful code has a big impact on understanding: and as result on productivity.

The language offers useful syntax to improve the readability using argument labels, or to access a tuple or structure component.

For instance the following function returns a tuple that contains the minimum and maximum values from the provided two arguments:

The invocation getMinMax(a: 15, b: 8) has a and b as argument labels. These indicate additional information about the arguments.

Every medal has two sides. There are situations where verbose code becomes redundant. It talks too much than it's necessary.

Thinking better about the previous example, it is possible that a and b argument labels do not provide useful information. And suppose you want to access only the minimum from the returned tuple let (min, max) = getMinMax().

For such cases Swift provides the wildcard pattern - a special underscore character _.

The function signature is changed to getMinMax(_ a: Int, _ b: Int), where _ is inserted before the parameter name. This means that argument labels are not necessary on invocation: getMinMax(15, 8).
Also the max component of the tuple can be skipped on extraction: let (min, _).

result.sub and result.div (or result.1 and result.3) selects tuple's named components sub and div.
The downside of this approach is the need to keep a variable for tuple. And later access the components using a selector.

3. Ignore enumeration associated value

Swift is so kind that besides simple enumeration, you can associate for each enumeration case a value. These are similar to tagged unions data structure from computer science.

Enumeration is a powerful data structure, especially in combination with associated values. However extracting data from such complex structure has some overhead, especially to access the associated values.

For instance let's enumerate the flight phases of an aircraft. An aircraft can:

Stay on the ground without movement (fixed)

It can accelerate with a specific speed before take-off (acceleration (Int))

The flight speed case .flight(let speed, _) omits the information about the altitude using an underscore _. Extracting the altitude in this case is simply not necessary.

Even if the altitude info is extracted .flight(let speed, let altitude), but later not used, Swift triggers a warning: immutable value 'altitude' was never used. The underscore helps solving such situations.

4. Skip function invocation result

Swift 3.0 implements an interesting mechanism that warns developer when a function invocation result (non Void) is not used.

.filter() method accepts a closure that receives index and value values on each iteration. Because the intent is to check whether the index is even, the value parameter is skipped using an underscore _: .filter { index, _ in ...}.

6. Skip iteration value

Another common usage of the underscore is to iterate a code block a particular number of times, and ignore the iteration value.

On a function invocation _ = myFunc() you can also suppress the usage of the returned value. However this workaround should be applied as an exception, since in most of the cases the result should be used.

Do you know other useful applications of the underscore in Swift? Feel free to write a comment bellow!