Swift’s gems: nil, Void and Never

Radu Dan
4 min readMay 18, 2019

Motivation

This article gives you a snippet of what is ‘nil’, ‘Void’ and ‘Never’ in Swift terminology.
We want to cover some basic usage of these three keywords so you will have a clear understanding of their differences.

nil

The absence of a value.

When we are saying nil, we are bringing to the table Swift’s optionals. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all” [1].

Under the hood an Optional is an enum:

Example:

Void

A return type that doesn’t return a value.

Void is an empty tuple (). It is implemented with a typealias:

typealias Void = ()

The usage is for functions that don’t return values. This is the default return type for functions or closures if not specified otherwise.

Example 1

It is the same thing as:

Example 2

Example 3

In this example, we have a closure that takes an argument which type is Void and returns Void (no values).
Could we have written as below?

completion: (Void) -> ())

Yes, but if you are using Swift 4 or above, we have a compile warning:

The compiler wants us to remove the Void. As the language evolved, the tuples are now handled with more restrictions.

But is completion: (Void) -> ()’ the same as completion: () -> ()?

Let’s see:

And call the methods from outside:

First closure doSomeStuff returns a parameter which value is Void. The second closure doesn’t return anything.

You can try writing result in for second closure, but the compiler will now give you an error:

Never

Something that will never return.

Never appeared in Swift 3 and it removed the `@noreturn` attribute and introduced an empty type.
It is implemented as an empty enum:

enum Never {}

You can read the proposal here: https://github.com/apple/swift-evolution/blob/master/proposals/0102-noreturn-bottom-type.md.
It helps the compiler to understand which functions will never return. This brings the discussion of how Never is different from Void.

Void vs Never

  • Implementation: Void is a tuple, Never is an empty enum.
  • Never informs the compiler that there is no need to return a value.
  • Never is used in methods that will unconditionally throw an error or crash the system.
  • Never can’t be constructed, is an uninhabited type [2].

Common usages:

Example 1 — fatalError

Example 2 — preconditionFailure

Conclusion

We saw some common usages of Swift’s: nil, Void and Never.
The most uncommon one is Never, it is rarely used in apps and at a first glance you can say is the same as Void, but we saw together that this is not the case.

Optionals are at the heart of Swift APIs, they are used everywhere and it is important to understand why and what they are solving. I recommend a great article if you are a beginner and want to understand them better: https://hackernoon.com/swift-optionals-explained-simply-e109a4297298.

Void I believe you heard of it from different languages, such as C/C++ and it means that the function doesn’t return a value (a return type without values).

  • nil is the absence of a value
  • Void is a return type that doesn’t return a value
  • Never is something that will never return

You can find me on Twitter @radude89 or on GitHub: radude89 .

This article can also be found here.

References

There are some great articles out there that go more in depth:

--

--