Before we start though, we should specify what we mean by auto-derivation. Auto-derivation is often confused for plain old derivation, so here are some examples to tell them apart.
For these examples, assume that there is a typeclass defined as:
The following is plain old manual instance definition. There is no derivation of any kind and we must write out exactly how JsonParser works for User.
Next is simple, manual derivation, NOT automatic. Notice that you have to call
deriveInstance, but you don't have to specify how it is derived. Some macro system is likely used underneath.
Type classes in Scala are interesting, in that even though they aren’t part of the language, they are widely used. This is mostly attributed to type classes’ usefulness and the ability for Scala to encode many concepts in its free-form implicit system. This free-form system has its benefits, but can also allow for complexity and unexpected bugs to arise if not used carefully. One issue that can arise quickly in Scala is the overuse of Orphan Instances for type-classes.
Type classes first appeared in Haskell as a way to implement overloading in a principled way. The idea is simple: instead…
Type variables are a powerful piece of type-level programming. They can appear in a variety of forms and factors; phantoms and existential types being two important cases. In this article we will be exploring existential types via a specific use case you may encounter in production code.
But before we can talk about existential types, we must talk about type variables in general.
Normally, this is the form in which we see type variables:
type F[A] = SomeClass[A]
A is said to be a "type variable". Note that
A appears on both sides of the equation: on
F and on…