How are optional values implemented in Swift?

前端 未结 4 445
广开言路
广开言路 2020-12-30 01:21

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 t

4条回答
  •  梦毁少年i
    2020-12-30 02:08

    Optionals are implemented as shown below. To find this, CMD-Click on a declaration like var x: Optional. var x: Int? is just syntactic sugar for that.

    enum Optional : LogicValue, Reflectable {
        case None
        case Some(T)
        init()
        init(_ some: T)
    
        /// Allow use in a Boolean context.
        func getLogicValue() -> Bool
    
        /// Haskell's fmap, which was mis-named
        func map(f: (T) -> U) -> U?
        func getMirror() -> Mirror
    }
    

提交回复
热议问题