/* Optional Until we have working oneof types, we have this library-based oneof. */ // Users should never touch this type directly. struct __NoneType { func getLogicValue() -> Bool { return false } } func [prefix] !(x: __NoneType) -> Bool { return true } extension __NoneType : Equatable { func __equal__(rhs: __NoneType) -> Bool { return true } } // This constant is for public consumption var None: __NoneType { return __NoneType() } /* // // FIXME: Until Swift supports partial ordering of generic functions, // these will cause ambiguities against more useful overloads like // those interactions with Optional. Keep them on ice until then. // func == (lhs: __NoneType, rhs: T) -> Bool { return rhs.isNone() } func != (lhs: __NoneType, rhs: Optional) -> Bool { return !rhs.isNone() } func == (lhs: Optional, rhs: __NoneType) -> Bool { return lhs.isNone() } func != (lhs: Optional, rhs: __NoneType) -> Bool { return !lhs.isNone() } */ struct Optional: Enumerable, LogicValue { typealias EnumeratorType = Slice func getEnumeratorType() -> Slice { return value } constructor() {} constructor(x: T) { value = new T[1] value[0] = x } constructor(x: __NoneType) {} /// \brief Allow use in a Boolean context. func getLogicValue() -> Bool { return value.length > 0 } func isNone() -> Bool { return !getLogicValue() } func get() -> T { assert(value.length > 0) return value[0] } var value: T[] } // Emulate .Some(x) tag constructor to be delivered by oneof func Some(x: T) -> Optional { return Optional(x) } // FIXME: Has no effect pending extension __NoneType { func [conversion] __conversion () -> Optional { return Optional(None) } } func [prefix] ! (x: Optional) -> Bool { return !x.getLogicValue() } func == (lhs: __NoneType, rhs: Optional) -> Bool { return rhs.isNone() } func != (lhs: __NoneType, rhs: Optional) -> Bool { return !rhs.isNone() } func == (lhs: Optional, rhs: __NoneType) -> Bool { return lhs.isNone() } func != (lhs: Optional, rhs: __NoneType) -> Bool { return !lhs.isNone() }