When are higher kinded types useful?

后端 未结 5 1380
一向
一向 2021-01-29 18:21

I\'ve been doing dev in F# for a while and I like it. However one buzzword I know doesn\'t exist in F# is higher-kinded types. I\'ve read material on higher-kinded types, and

5条回答
  •  -上瘾入骨i
    2021-01-29 19:14

    I don't want to repeat information in some excellent answers already here, but there's a key point I'd like to add.

    You usually don't need higher-kinded types to implement any one particular monad, or functor (or applicative functor, or arrow, or ...). But doing so is mostly missing the point.

    In general I've found that when people don't see the usefulness of functors/monads/whatevers, it's often because they're thinking of these things one at a time. Functor/monad/etc operations really add nothing to any one instance (instead of calling bind, fmap, etc I could just call whatever operations I used to implement bind, fmap, etc). What you really want these abstractions for is so you can have code that works generically with any functor/monad/etc.

    In a context where such generic code is widely used, this means any time you write a new monad instance your type immediately gains access to a large number of useful operations that have already been written for you. That's the point of seeing monads (and functors, and ...) everywhere; not so that I can use bind rather than concat and map to implement myFunkyListOperation (which gains me nothing in itself), but rather so that when I come to need myFunkyParserOperation and myFunkyIOOperation I can re-use the code I originally saw in terms of lists because it's actually monad-generic.

    But to abstract across a parameterised type like a monad with type safety, you need higher-kinded types (as well explained in other answers here).

提交回复
热议问题