After the disclaimer
Type class authors will most likely use higher-level derivation or generic programming libraries to implement
, the documentation on type class derivation proceeds to show an advanced low-level example about how to write
How could the example below be completed, as straightforwardly as possible, and without lower-level facilities?
extension (t: T) def show: String
inline def derived[T <: Product : Mirror.ProductOf]: Showable[T] =
extension (t: T) def show: String = ???
Or, if that incomplete example is somehow wrong or too hard, could anyone provide another basic example on how to write custom type class derivations?
I think I went on a similar journey a while back.
I used this blog post, which may now be out of date,
and set myself the task to derive an html table, from simple case class shapes with mirrors only. The (somewhat questionable) result is here;
I believe it is an example of some exploring the same sort of space, that you want to experiment in - i.e. derviation without full on quote / splice meta-programming? I would not wish you to believe, that where I ended is particularly correct or useful, it has not been peer reviewed by someone else and don’t consider myself competent in this meta-programming space. So there are some caveats and I also hope, that you have more answers are on the way :-)!
However, it could be that if you take scautable apart and see how it works, it may provide some ideas - discussion on the repo are always open :-)!
Thanks! With the help of the blog post, I was eventually able to write a generic
derived function. It looks like the “low-level” stuff is unavoidable in such cases (
The documentation mentions using “generic programming libraries” for higher level solutions. (Those are not part of the scala standard library and hence are not documented within the documentation of the language.)
One example of such libraries would be shapeless 3.
An example for deriving
Show with shapeless 3 can be found here.