|
| 1 | +import scala.quoted.* |
| 2 | +import scala.deriving.Mirror |
| 3 | + |
| 4 | +// derivation code is a slightly modified version of: https://github.com/lampepfl/dotty-macro-examples/blob/main/macroTypeClassDerivation/src/macro.scala |
| 5 | +object Derivation { |
| 6 | + |
| 7 | + // Typeclass instance gets constructed as part of a macro |
| 8 | + inline given deriveFullyConstrucedByMacro[A](using Mirror.ProductOf[A]): Show[A] = Derivation.deriveShow[A] |
| 9 | + |
| 10 | + // Typeclass instance is built inside as part of a method, only the 'show' impl is filled in by a macro |
| 11 | + inline given derivePartiallyConstructedByMacro[A](using Mirror.ProductOf[A]): Show[A] = |
| 12 | + new { |
| 13 | + def show(value: A): String = Derivation.show(value) |
| 14 | + } |
| 15 | + |
| 16 | + inline def show[T](value: T): String = ${ showValue('value) } |
| 17 | + |
| 18 | + inline def deriveShow[T]: Show[T] = ${ deriveCaseClassShow[T] } |
| 19 | + |
| 20 | + private def deriveCaseClassShow[T](using quotes: Quotes, tpe: Type[T]): Expr[Show[T]] = { |
| 21 | + import quotes.reflect.* |
| 22 | + // Getting the case fields of the case class |
| 23 | + val fields: List[Symbol] = TypeTree.of[T].symbol.caseFields |
| 24 | + |
| 25 | + '{ |
| 26 | + new Show[T] { |
| 27 | + override def show(t: T): String = |
| 28 | + ${ showValue('t) } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + def showValue[T: Type](value: Expr[T])(using Quotes): Expr[String] = { |
| 34 | + import quotes.reflect.* |
| 35 | + |
| 36 | + val fields: List[Symbol] = TypeTree.of[T].symbol.caseFields |
| 37 | + |
| 38 | + val vTerm: Term = value.asTerm |
| 39 | + val valuesExprs: List[Expr[String]] = fields.map(showField(vTerm, _)) |
| 40 | + val exprOfList: Expr[List[String]] = Expr.ofList(valuesExprs) |
| 41 | + '{ "{ " + $exprOfList.mkString(", ") + " }" } |
| 42 | + } |
| 43 | + |
| 44 | + /** Create a quoted String representation of a given field of the case class */ |
| 45 | + private def showField(using Quotes)(caseClassTerm: quotes.reflect.Term, field: quotes.reflect.Symbol): Expr[String] = { |
| 46 | + import quotes.reflect.* |
| 47 | + |
| 48 | + val fieldValDef: ValDef = field.tree.asInstanceOf[ValDef] |
| 49 | + val fieldTpe: TypeRepr = fieldValDef.tpt.tpe |
| 50 | + val fieldName: String = fieldValDef.name |
| 51 | + |
| 52 | + val tcl: Term = lookupShowFor(fieldTpe) // Show[$fieldTpe] |
| 53 | + val fieldValue: Term = Select(caseClassTerm, field) // v.field |
| 54 | + val strRepr: Expr[String] = applyShow(tcl, fieldValue).asExprOf[String] |
| 55 | + '{ ${ Expr(fieldName) } + ": " + $strRepr } // summon[Show[$fieldTpe]].show(v.field) |
| 56 | + } |
| 57 | + |
| 58 | + /** Look up the Show[$t] typeclass for a given type t */ |
| 59 | + private def lookupShowFor(using Quotes)(t: quotes.reflect.TypeRepr): quotes.reflect.Term = { |
| 60 | + import quotes.reflect.* |
| 61 | + t.asType match { |
| 62 | + case '[tpe] => |
| 63 | + Implicits.search(TypeRepr.of[Show[tpe]]) match { |
| 64 | + case res: ImplicitSearchSuccess => res.tree |
| 65 | + case failure: DivergingImplicit => report.errorAndAbort(s"Diverving: ${failure.explanation}") |
| 66 | + case failure: NoMatchingImplicits => report.errorAndAbort(s"NoMatching: ${failure.explanation}") |
| 67 | + case failure: AmbiguousImplicits => report.errorAndAbort(s"Ambiguous: ${failure.explanation}") |
| 68 | + case failure: ImplicitSearchFailure => |
| 69 | + report.errorAndAbort(s"catch all: ${failure.explanation}") |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** Composes the tree: $tcl.show($arg) */ |
| 75 | + private def applyShow(using Quotes)(tcl: quotes.reflect.Term, arg: quotes.reflect.Term): quotes.reflect.Term = { |
| 76 | + import quotes.reflect.* |
| 77 | + Apply(Select.unique(tcl, "show"), arg :: Nil) |
| 78 | + } |
| 79 | +} |
0 commit comments