diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-11-10 15:23:38 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-10 15:23:38 +0000 |
commit | 9c90d3f393d18ef0f8f27751a8328913dad7ad25 (patch) | |
tree | 345f7f8a497f5fc12e2aaad9cf9e8119e0059d62 /examples/src/main/scala/show.scala | |
parent | 657d2fb2c16b75c790bf3c3a63d1f830a42893db (diff) | |
parent | 2ae9a65ed0b773f77b5cd8d8c134a125bbaccf5d (diff) | |
download | magnolia-9c90d3f393d18ef0f8f27751a8328913dad7ad25.tar.gz magnolia-9c90d3f393d18ef0f8f27751a8328913dad7ad25.tar.bz2 magnolia-9c90d3f393d18ef0f8f27751a8328913dad7ad25.zip |
Merge pull request #27 from propensive/allow-type-constructor-in-supertrait
Allow type constructor in supertrait
Diffstat (limited to 'examples/src/main/scala/show.scala')
-rw-r--r-- | examples/src/main/scala/show.scala | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/examples/src/main/scala/show.scala b/examples/src/main/scala/show.scala index e9e74b1..e101c11 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] } |