Haskell's deriving construct is both wonderful and magical at the same time: it is wonderful, because with just a single line of code, you can obtain functionality such as(de)serialization, comparison and traversal functions for your newly defined datatype; it is magical because all of this happens behind the scenes, by code deeply built into the compiler.
But does it have to be magic? Since recently, the Glasgow Haskell Compiler (GHC) supports a feature that lets you define your own derivable functionality! And it is not difficult to use, either. All you have to do is to understand a little bit about how Haskell datatypes can be uniformly represented using a limited set of simple datatypes. Then you define a class plus instances for this limited set of types, and get all the rest for free.
So-called datatype-generic functions let you properly express your code in terms of specific, user-defined datatypes without paying a price: for your new datatypes, you can still use lots of pre-defined functionality, simply by deriving it.
YOU MAY ALSO LIKE:
- Deriving Via (SkillsCast recorded in October 2018)
- FP in Kotlin with Arrow with Jorge Castillo (Online Course on 20th - 23rd September 2021)
- F# eXchange 2021 (Online Conference on 20th October 2021)
- ScalaCon 2021: November Edition (Online Conference on 2nd - 5th November 2021)
- Abstract Fun-sense: a functional perspective on life (SkillsCast recorded in May 2021)
- Journey to the Centre of the JVM (SkillsCast recorded in May 2021)
Datatype-Generic Programming in Haskell
Andres Löh is a Haskell consultant and co-owner of Well-Typed LLP. He is based in Regensburg, Germany. He started using Haskell in 1997, when being an undergraduate student of mathematics in Konstanz and has been an enthusiastic functional programmer ever since. Andres obtained a PhD in Computer Science from Utrecht University in 2004, on extending the Haskell language with capabilities for datatype-generic programming. After having been a university lecturer for several years, he joined Well-Typed in 2010.