Hacker News new | ask | show | jobs
by cballard 3763 days ago
Both Optional and Array have flatMap, i.e.

    T?.flatMap(T -> R?) -> R?
    [T].flatMap(T -> [R]) -> [R]
However, you cannot write a protocol for "things that can be flatMapped", because you cannot refer to a Self<T> specialized with a different type (Self<R>). With HKTs, you can do that, which would let you write something like this:

    protocol FlatMappable
    {
        typealias Value
        func flatMap<R>(Value -> Self<where Value == R>) -> Self<where Value == R>
    }
That's not useful on its own, but it means that you can now write functions and extensions that reason about the general concept of anything FlatMappable (this is most of the way to what is known as monads, FWIW).