Articles, podcasts and news about Swift development, by John Sundell.

Using map to transform an optional into a Result type

Published on 12 Aug 2017
Basics article available: Map, FlatMap and CompactMap

Using map you can transform an optional value into an optional Result type by simply passing in the enum case.

enum Result<Value> {
    case value(Value)
    case error(Error)
}

class Promise<Value> {
    private var result: Result<Value>?
    
    init(value: Value? = nil) {
        result = value.map(Result.value)
    }
}