Hacker News new | ask | show | jobs
by cballard 3764 days ago
Big things missing from Swift IMO:

- Higher-kinded types

- Strictly-typed "throw"

I'm not sure about dropping "Type" from protocols because it's pretty commonly used to separate the protocol definition (which is extended to provide most of the implementation) from the concrete base implementation, e.g. RAC's SignalProducerType/SignalProducer.

2 comments

Would you have an example for someone who doesn’t know anything about higher-kinded types?
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).
Swift protocols are begging for HKTs.