PoC for Generic Typeclasses in Dotty

Mark "Justin" Waks
2 min readNov 23, 2018

--

(This is very early, just a preliminary experiment, but I wanted to bookmark it for myself and might as well bring it to other folks’ attention. If you’re interested in Dotty and Shapeless, this one’s worth taking a look at.)

Martin has just committed a proof-of-concept experiment in generic typeclass derivation for Dotty. This sort of thing is pretty much the heart and soul of Shapeless — taking a complex type (most often an ADT, sometimes a very complex one), transforming it to a “generic” form, and providing typeclass instances for the various shapes of that generic. This is how Shapeless lets you magically create picklers and suchlike for arbitrary case classes.

It’s a terribly important topic for Dotty — many of us count on Shapeless, whether we realize it or not, for the more sophisticated libraries that let us do really complex actions in low-boilerplate ways. Since Dotty throws out Scala 2 macros, and is replacing them with new tools for metaprogramming, it hasn’t been immediately obvious whether Shapeless-like tools could be built.

The current experiment shows two potential ways to write Shapeless-style generics in Dotty. Martin specifically calls out the second approach, which is worth reading through if you’ve been paying attention to Dotty — it’s a nice deep example of Dotty code, including enums, inlining, and match types. It demonstrates simple generic-style typeclasses for pickling and equality.

(Related: Dotty 0.10.0 has just been released, and the documentation is essential for understanding the ins and outs of this code. I particularly call to your attention the sections on inlining and match types, both of which are new in this release, and which are essential for understanding this code — they’re both heavily used.)

It’s far from production-quality yet: the typeclasses themselves are boilerplatey, and this experiment doesn’t yet handle real case class hierarchies. But it’s a fine initial proof of concept, illustrating some possible ways forward for this crucial Scala capability. Personally, I think it’s very exciting, showing that Dotty’s new metaprogramming systems should suffice for one of the more important use cases…

--

--

Mark "Justin" Waks
Mark "Justin" Waks

Written by Mark "Justin" Waks

Lifelong programmer and software architect, specializing in online social tools and (nowadays) Scala. Architect of Querki (“leading the small data revolution”).

No responses yet