diff options
Diffstat (limited to 'examples/shared/src/main/scala/show.scala')
-rw-r--r-- | examples/shared/src/main/scala/show.scala | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/examples/shared/src/main/scala/show.scala b/examples/shared/src/main/scala/show.scala index 6f5838b..74914cb 100644 --- a/examples/shared/src/main/scala/show.scala +++ b/examples/shared/src/main/scala/show.scala @@ -20,26 +20,27 @@ trait GenericShow[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[Out, T] = new Show[Out, T] { - def show(value: T) = - if (ctx.isValueClass) { - val param = ctx.parameters.head - param.typeclass.show(param.dereference(value)) - } else { - val paramStrings = ctx.parameters.map { param => - s"${param.label}=${param.typeclass.show(param.dereference(value))}" + def combine[T](ctx: CaseClass[Typeclass, T]): Show[Out, T] = (value: T) => { + if (ctx.isValueClass) { + val param = ctx.parameters.head + param.typeclass.show(param.dereference(value)) + } else { + val paramStrings = ctx.parameters.map { param => + val attribStr = if(param.annotations.isEmpty) "" else { + param.annotations.mkString("{", ", ", "}") } - - join(ctx.typeName.short, paramStrings) + s"${param.label}$attribStr=${param.typeclass.show(param.dereference(value))}" } + + join(ctx.typeName.short, paramStrings) + } } /** choose which typeclass to use based on the subtype of the sealed trait */ - def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[Out, T] = new Show[Out, T] { - def show(value: T): Out = ctx.dispatch(value) { sub => + def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[Out, T] = (value: T) => + 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] @@ -49,17 +50,13 @@ trait GenericShow[Out] { 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 - } + implicit val string: Show[String, String] = (s: 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 - } + implicit val int: Show[String, Int] = (s: Int) => s.toString /** show typeclass for sequences */ implicit def seq[A](implicit A: Show[String, A]): Show[String, Seq[A]] = |