Swift – How Are Optional Values Implemented?

option-typeswift

I wonder how the value types in Swift (Int, Float…) are implemented to support optional binding ("?").
I assume those value types are not allocated on the heap, but on the stack. So, do they rely on some kind of pointer to the stack that may be null, or does the underlying struct contain a boolean flag ?

Best Answer

Optionals are implemented as enum type in Swift.

See Apple's Swift Tour for an example of how this is done:

enum OptionalValue<T> {
    case None
    case Some(T)
}
Related Question