This is just a variation of 'any problem can be solved by adding a level of indirection' + 'any protocol can have a dummy implementation'.
This is just a variation of 'any problem can be solved by adding a level of indirection' + 'any protocol can have a dummy implementation'.
> What if you want to write a library that
works with either blocking calls or
asynchronous promises?
You don't need higher-kinded types for that.
Just always return a promise, except the
blocking version always return a promise that
is already fulfilled.
While strictly speaking this approach will work, a benefit of employing higher-kinded types (HTK's) to express a solution is being able to optimize without having to alter the solution.For this example, the Scalaz Id[0] type provides this adaptation. Since it conforms to what is expected of a container, yet does not require the overhead of fabricating one just to satisfy expectations, it affords HKT-based logic to be useful while automatically selecting the optimal implementation.
In short, working in an environment which supports HKT's often allows implementations to reduce needless overhead, simplify implementations (by only having to address "happy path" logic), and promotes stability in a code base due to formally expressing the expectations of collaborators.
I.e. you've pulled the side effects (blocking) from the future to the present.
Consider a command line IM app using pipes to communicate to the server (just to roll with the readLine example). User 1 has not typed anything, so their file is empty. User 2 has typed a bunch of stuff.
user1line = readLine "user1file.pipe"
user2line = readLine "user2file.pipe"
getFirstAvailableFutureAndProcess [user1line, user2line] processorFunc
What you want this to do is handle whichever user types something first. In reality it will only process user1line.And that's the point of a lot of these abstractions. It's not about being able to write something that you couldn't in another language. After all, we could create the same functionality in assembly.
[0] https://hackage.haskell.org/package/transformers-0.2.2.1/doc...