[swift-evolution] [Proposal] Higher Kinded Types (Monads, Functors, etc.)

Will Fancher willfancher38 at gmail.com
Wed Dec 16 15:10:11 CST 2015

With respect specifically to Monads, there are numerous useful abstractions. For one, Applicative and Functor can be derived for free, requiring the no effort on the Monad instance's part. This alone eliminates a lot of code repetition. Additionally, the large majority of functions described for Haskell here are very useful:


Those functions come as abstractions over monads which can be easily understood (if you understand monads) and which eliminate a lot of code repetition. The existence of these abstractions makes any Monad type much more robust that it would be on its own.

There are plenty of typeclasses in Haskell that could be implemented in Swift and would be useful if we had HKTs. Foldables and Traversables, for example, are simple and powerful abstractions that would be well worth having. And there are various simpler things besides Haskell typeclasses that can't be done in Swift as it stands. For example, you can't currently take a collection of type C1: CollectionType, and return a value of type C2 where C2 ~= C1, which means you can't perform arbitrary transformations on arbitrary collections. You have to choose an implementation type and use that one.

This isn't to say this should all be in the standard library. I think most of these protocols would be best off in a separate package, to keep the standard simple for newcomers and for those who don't need these features. So I'm not suggesting that the Swift team create Monad, Functor, etc. in the standard library and then extend all the appropriate types to implement those (although it'd be a bonus if the team wanted to). That job would be up to whoever wants to write the package for it. But, in my opinion, the ability for such a package to exist seems very important.

More information about the swift-evolution mailing list