What is the Scala Kind Projector?

Thu, Jul 25, 2019

A quick introduction to the Kind Projector compiler plugin

I will give you a similar warning to the one in the github repository:

You must be mad, otherwise you wouldn't have come here

Kind Projector is a compiler plugin, it adds a new syntax for type lambdas. In the same way we can define functions inline, in scala we can also define type inline. Type lambdas don’t look really appealing to the eye, but here is an example:

10.pure[({type T[A] = Either[String, A]})#T]
/* scala.util.Either[String,Int] = Right(10) */

Applicative#pure expects a higher kind type F[_], that is why we can just use Either[_,_], we need to fix at least one of the types. If we wanted to do this without type lambdas, that looks like this:

type EStrOr[A] = Either[String, A]
/* EStrOr[Int] = Right(10) */

It looks more readable but we are creating a type to just be able to use Either[_,_] where a F[_] is required. Wouldn’t it be nice to not have to write custom types nor a type lambda by hand? Yes!

10.pure[Either[String, ?]]
/* scala.util.Either[String,Int] = Right(10) */

did you notice? we created a type lambda by just typing a ?.

This syntax is the one we used in the previous example, we added a question mark ? where we needed it. We also can use covariance +? and contravariance -? if needed.

Either[String, ?]          /* <-> T[A] = Either[String, A] */
EitherT[Option, String, ?] /* <-> T[A] = EitherT[Option, String, T] */
EitherT[?[_], String, ?]   /* <-> T[F[_], A] = EitherT[F, String, T] */
Tuple3[-?, String, +?]     /* <-> T[-A, +B] = Tuple3[A, String, B] */

What happens if we wanted to write Tuple2[?,?] where ? is the same for the two types parameter? We can’t with the inline syntax. For more advanced uses of the kind projector we should use the function syntax.