How are optional values implemented in Swift?

option-type, swift

Solution

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)
}

Problem

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 ?

Original source

Related problems