blob: 55b0753d7ddf092be661598ea982644b400000e6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package magnolia.examples
import scala.language.existentials
import scala.language.higherKinds
import magnolia._
import scala.language.experimental.macros
trait Default[T] { def default: T }
object Default {
type Typeclass[T] = Default[T]
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: 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 gen[T]: Default[T] = macro Magnolia.gen[T]
}
|