aboutsummaryrefslogblamecommitdiff
path: root/examples/src/main/scala/default.scala
blob: 55b0753d7ddf092be661598ea982644b400000e6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                         

                                                                                             

   
                                                                                




                                                                                 
                                                         
 
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]
}