From a19b4d22b4caffbed551182b20657588b470230b Mon Sep 17 00:00:00 2001 From: Jon Pretty Date: Fri, 10 Nov 2017 14:57:55 +0000 Subject: Attempt to allow the `Typeclass` definition to be defined in supertrait --- core/src/main/scala/magnolia.scala | 12 ++++++++++-- examples/src/main/scala/show.scala | 30 +++++++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/src/main/scala/magnolia.scala b/core/src/main/scala/magnolia.scala index 293d0f1..f7e9b0e 100644 --- a/core/src/main/scala/magnolia.scala +++ b/core/src/main/scala/magnolia.scala @@ -71,8 +71,16 @@ object Magnolia { val magnoliaObj = q"$magnoliaPkg.Magnolia" val arrayCls = tq"_root_.scala.Array" - val typeConstructor: c.Type = - c.prefix.tree.tpe.member(TypeName("Typeclass")).asType.toType.typeConstructor + val typeDefinitions = + c.prefix.tree.tpe.baseClasses.flatMap(_.asType.toType.decls).filter(_.isType) + + val typeConstructorOpt = + typeDefinitions.find(_.name.toString == "Typeclass").map(_.asType.toType.typeConstructor) + + val typeConstructor = typeConstructorOpt.getOrElse { + c.abort(c.enclosingPosition, "magnolia: the derivation object does not define the Typeclass "+ + "type constructor") + } def findType(key: Type): Option[TermName] = recursionStack(c.enclosingPosition).frames.find(_.genericType == key).map(_.termName(c)) diff --git a/examples/src/main/scala/show.scala b/examples/src/main/scala/show.scala index e9e74b1..a83c509 100644 --- a/examples/src/main/scala/show.scala +++ b/examples/src/main/scala/show.scala @@ -9,43 +9,51 @@ import scala.language.experimental.macros * be something other than a string. */ trait Show[Out, T] { def show(value: T): Out } -/** companion object to [[Show]] */ -object Show { - +trait GenericShow[Out] { + /** the type constructor for new [[Show]] instances * * The first parameter is fixed as `String`, and the second parameter varies generically. */ - type Typeclass[T] = Show[String, T] + type Typeclass[T] = Show[Out, T] + + def join(typeName: String, strings: Seq[String]): Out /** creates a new [[Show]] instance by labelling and joining (with `mkString`) the result of * showing each parameter, and prefixing it with the class name */ - def combine[T](ctx: CaseClass[Typeclass, T]): Show[String, T] = new Show[String, T] { + def combine[T](ctx: CaseClass[Typeclass, T]): Show[Out, T] = new Show[Out, T] { def show(value: T) = { val paramStrings = ctx.parameters.map { param => s"${param.label}=${param.typeclass.show(param.dereference(value))}" } - paramStrings.mkString(s"${ctx.typeName.split("\\.").last}(", ",", ")") + join(ctx.typeName.split("\\.").last, paramStrings) } } /** choose which typeclass to use based on the subtype of the sealed trait */ - def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[String, T] = new Show[String, T] { - def show(value: T): String = ctx.dispatch(value) { sub => + def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[Out, T] = new Show[Out, T] { + def show(value: T): Out = ctx.dispatch(value) { sub => sub.typeclass.show(sub.cast(value)) } } + /** bind the Magnolia macro to this derivation object */ + implicit def gen[T]: Show[Out, T] = macro Magnolia.gen[T] +} + +/** companion object to [[Show]] */ +object Show extends GenericShow[String] { + /** show typeclass for strings */ implicit val string: Show[String, String] = new Show[String, String] { def show(s: String): String = s } + def join(typeName: String, params: Seq[String]): String = + params.mkString(s"$typeName(", ",", ")") + /** show typeclass for integers */ implicit val int: Show[String, Int] = new Show[String, Int] { def show(s: Int): String = s.toString } - - /** bind the Magnolia macro to this derivation object */ - implicit def gen[T]: Show[String, T] = macro Magnolia.gen[T] } -- cgit v1.2.3