aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2017-11-10 14:57:55 +0000
committerJon Pretty <jon.pretty@propensive.com>2017-11-10 14:57:55 +0000
commita19b4d22b4caffbed551182b20657588b470230b (patch)
tree7a35fcdc604794650196479f973828771725ee71
parent657d2fb2c16b75c790bf3c3a63d1f830a42893db (diff)
downloadmagnolia-a19b4d22b4caffbed551182b20657588b470230b.tar.gz
magnolia-a19b4d22b4caffbed551182b20657588b470230b.tar.bz2
magnolia-a19b4d22b4caffbed551182b20657588b470230b.zip
Attempt to allow the `Typeclass` definition to be defined in supertrait
-rw-r--r--core/src/main/scala/magnolia.scala12
-rw-r--r--examples/src/main/scala/show.scala30
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]
}