aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/scala/show.scala
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2017-11-09 16:34:18 +0000
committerJon Pretty <jon.pretty@propensive.com>2017-11-09 16:34:18 +0000
commit5d862115bd31fcd42484293c1f64652192d95d26 (patch)
tree1e5301cd51192de114db20616e1c18e3d2726a52 /examples/src/main/scala/show.scala
parente396b7a038e458de37ced6b59e0d367883bc3b71 (diff)
downloadmagnolia-5d862115bd31fcd42484293c1f64652192d95d26.tar.gz
magnolia-5d862115bd31fcd42484293c1f64652192d95d26.tar.bz2
magnolia-5d862115bd31fcd42484293c1f64652192d95d26.zip
Upgrade to SBT 1.0 and include testing binariesv0.5.0
Diffstat (limited to 'examples/src/main/scala/show.scala')
-rw-r--r--examples/src/main/scala/show.scala38
1 files changed, 24 insertions, 14 deletions
diff --git a/examples/src/main/scala/show.scala b/examples/src/main/scala/show.scala
index 860871e..6ed0ff2 100644
--- a/examples/src/main/scala/show.scala
+++ b/examples/src/main/scala/show.scala
@@ -7,38 +7,48 @@ import magnolia._
import scala.language.experimental.macros
/** shows one type as another, often as a string
- *
- * Note that this is a more general form of `Show` than is usual, as it permits the return type to
- * be something other than a string. */
+ *
+ * Note that this is a more general form of `Show` than is usual, as it permits the return type to
+ * be something other than a string. */
trait Show[Out, T] { def show(value: T): Out }
/** companion object to [[Show]] */
object Show {
/** the type constructor for new [[Show]] instances
- *
- * The first parameter is fixed as `String`, and the second parameter varies generically. */
+ *
+ * The first parameter is fixed as `String`, and the second parameter varies generically. */
type Typeclass[T] = Show[String, T]
/** creates a new [[Show]] instance by labelling and joining (with `mkString`) the result of
- * showing each parameter, and prefixing it with the class name */
+ * 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 show(value: T) = ctx.parameters.map { param =>
- s"${param.label}=${param.typeclass.show(param.dereference(value))}"
- }.mkString(s"${ctx.typeName.split("\\.").last}(", ",", ")")
+ 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}(", ",", ")")
+ }
}
/** 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 => sub.typeclass.show(sub.cast(value)) }
+ def show(value: T): String = ctx.dispatch(value) { sub =>
+ sub.typeclass.show(sub.cast(value))
+ }
}
/** 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] = new Show[String, String] {
+ def show(s: String): String = s
+ }
+
/** 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] = 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]
}