Trying to make sense of the code below. I understand that T is passed by when instantiating the Optional, as in Optional, but what about the U type in map. What type does that assume?
enum Optional<T> : 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<U>(f: (T) -> U) -> U?
    func getMirror() -> Mirror
} 
The type U comes from the f parameter to the map function. So if you pass a closure that returns a Int, then map returns a Int?. If you pass a closure that returns a Array<Int>, then map returns a Array<Int>?.
For example, try this:
var opt1: Optional<Int> = .Some(1)
var opt2 = opt1.map { (i: Int) -> String in return "String: \(i)" }
You'll find that opt1 is an Int? and opt2 is a String?.
When calling the map function the caller must provide a single argument which is a closure that: 
Optional, i.e. T  U will then be the type of said return value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With