2 Minute Tips: The Dark Secrets of Bools
The Swift Standard Library Strikes Again
I mostly post to Substack these days. Subscribe now to receive non-paywalled, ludicrously in-depth articles on iOS, Swift, and indie projects every 2 weeks.
Booleans are the simplest type in the Swift language.
They’re simply true
or false
. The most basic language primitive you could imagine.
I was so naïve.
We only see the public interface of the Swift Bool
. Much like seeing a sausage made; vivisecting its innards in the source code might put you off forever.
A Frozen Struct
// Bool.swift
@frozen
public struct Bool: Sendable {
// ...
That’s right. Like all the basic Swift types you know and love, Bool
is a struct masquerading as a native language feature.
It’s marked @frozen
— so instance properties can’t be added or removed. This ensures the memory layout (i.e. number of bits) used is constant between Standard Library versions, which is critical for ABI stability.
Bool
is Sendable
, so the compiler doesn’t throw a fit when you to pass it between threads.
Let’s keep spelunking through the type.