diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-11-08 11:17:53 +0000 |
---|---|---|
committer | Jon Pretty <jon.pretty@propensive.com> | 2017-11-08 11:17:53 +0000 |
commit | 3f23cb5bca4ea8be889b714008a85141fe5e213c (patch) | |
tree | 8ba57fbf39c8f1892fc6b7adaf7810665e86de6c /examples/src/main/scala | |
parent | 1207383551b429560ac05e124349b24ff8b6dfda (diff) | |
download | magnolia-3f23cb5bca4ea8be889b714008a85141fe5e213c.tar.gz magnolia-3f23cb5bca4ea8be889b714008a85141fe5e213c.tar.bz2 magnolia-3f23cb5bca4ea8be889b714008a85141fe5e213c.zip |
Added better scaladocs and provide default values
Diffstat (limited to 'examples/src/main/scala')
-rw-r--r-- | examples/src/main/scala/decode.scala | 7 | ||||
-rw-r--r-- | examples/src/main/scala/default.scala | 8 | ||||
-rw-r--r-- | examples/src/main/scala/eq.scala | 6 | ||||
-rw-r--r-- | examples/src/main/scala/show.scala | 6 |
4 files changed, 13 insertions, 14 deletions
diff --git a/examples/src/main/scala/decode.scala b/examples/src/main/scala/decode.scala index 1cc8a12..a5d1d67 100644 --- a/examples/src/main/scala/decode.scala +++ b/examples/src/main/scala/decode.scala @@ -12,18 +12,18 @@ object Decoder { implicit val string: Decoder[String] = new Decoder[String] { def decode(str: String): String = str } implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt } - implicit def generic[T]: Decoder[T] = macro Magnolia.generic[T] + implicit def gen[T]: Decoder[T] = macro Magnolia.gen[T] type Typeclass[T] = Decoder[T] - def join[T](ctx: JoinContext[Decoder, T]): Decoder[T] = new Decoder[T] { + def combine[T](ctx: CaseClass[Decoder, T]): Decoder[T] = new Decoder[T] { def decode(value: String) = { val (name, values) = parse(value) ctx.construct { param => param.typeclass.decode(values(param.label)) } } } - def dispatch[T](ctx: DispatchContext[Decoder, T]): Decoder[T] = new Decoder[T] { + def dispatch[T](ctx: SealedTrait[Decoder, T]): Decoder[T] = new Decoder[T] { def decode(param: String) = { val (name, values) = parse(param) val subtype = ctx.subtypes.find(_.label == name).get @@ -60,6 +60,5 @@ object Decoder { (name, parts(value.substring(end + 1, value.length - 1)).map(keyValue).toMap) } - } diff --git a/examples/src/main/scala/default.scala b/examples/src/main/scala/default.scala index 1f602f9..55b0753 100644 --- a/examples/src/main/scala/default.scala +++ b/examples/src/main/scala/default.scala @@ -10,15 +10,15 @@ trait Default[T] { def default: T } object Default { type Typeclass[T] = Default[T] - def join[T](ctx: JoinContext[Default, T]): Default[T] = new Default[T] { - def default = ctx.construct { param => param.typeclass.default } + def combine[T](ctx: CaseClass[Default, T]): Default[T] = new Default[T] { + def default = ctx.construct { param => param.default.getOrElse(param.typeclass.default) } } - def dispatch[T](ctx: DispatchContext[Default, T])(): Default[T] = new Default[T] { + def dispatch[T](ctx: SealedTrait[Default, T])(): Default[T] = new Default[T] { def default: T = ctx.subtypes.head.typeclass.default } implicit val string: Default[String] = new Default[String] { def default = "" } implicit val int: Default[Int] = new Default[Int] { def default = 0 } - implicit def generic[T]: Default[T] = macro Magnolia.generic[T] + implicit def gen[T]: Default[T] = macro Magnolia.gen[T] } diff --git a/examples/src/main/scala/eq.scala b/examples/src/main/scala/eq.scala index ec9be14..408f287 100644 --- a/examples/src/main/scala/eq.scala +++ b/examples/src/main/scala/eq.scala @@ -9,17 +9,17 @@ trait Eq[T] { def equal(value: T, value2: T): Boolean } object Eq { type Typeclass[T] = Eq[T] - def join[T](ctx: JoinContext[Eq, T]): Eq[T] = new Eq[T] { + def combine[T](ctx: CaseClass[Eq, T]): Eq[T] = new Eq[T] { def equal(value1: T, value2: T) = ctx.parameters.forall { param => param.typeclass.equal(param.dereference(value1), param.dereference(value2)) } } - def dispatch[T](ctx: DispatchContext[Eq, T]): Eq[T] = new Eq[T] { + def dispatch[T](ctx: SealedTrait[Eq, T]): Eq[T] = new Eq[T] { def equal(value1: T, value2: T): Boolean = ctx.dispatch(value1) { case sub => sub.typeclass.equal(sub.cast(value1), sub.cast(value2)) } } implicit val string: Eq[String] = new Eq[String] { def equal(v1: String, v2: String) = v1 == v2 } implicit val int: Eq[Int] = new Eq[Int] { def equal(v1: Int, v2: Int) = v1 == v2 } - implicit def generic[T]: Eq[T] = macro Magnolia.generic[T] + implicit def gen[T]: Eq[T] = macro Magnolia.gen[T] } diff --git a/examples/src/main/scala/show.scala b/examples/src/main/scala/show.scala index 9af828e..4594bff 100644 --- a/examples/src/main/scala/show.scala +++ b/examples/src/main/scala/show.scala @@ -10,17 +10,17 @@ trait Show[Out, T] { def show(value: T): Out } object Show { type Typeclass[T] = Show[String, T] - def join[T](ctx: JoinContext[Typeclass, T]): Show[String, T] = new Show[String, T] { + 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 dispatch[T](ctx: DispatchContext[Typeclass, T]): Show[String, T] = new Show[String, T] { + 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)) } } implicit val string: Show[String, String] = new Show[String, String] { def show(s: String): String = s } implicit val int: Show[String, Int] = new Show[String, Int] { def show(s: Int): String = s.toString } - implicit def generic[T]: Show[String, T] = macro Magnolia.generic[T] + implicit def gen[T]: Show[String, T] = macro Magnolia.gen[T] } |