aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/scala/example.scala
blob: a031fbe217006cf7ab97daeb0512b206421fa66f (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
25
26
27
28
package magnolia

import language.experimental.macros

trait Extractor[T] {
  def extract(src: String): T
}

object Extractor extends Extractor_1 {
  
  def apply[T](fn: String => T): Extractor[T] = new Extractor[T] {
    def extract(source: String): T = fn(source)
  }

  implicit val intExtractor: Extractor[Int] = Extractor(_.toInt)
  implicit val stringExtractor: Extractor[String] = Extractor(identity)
  implicit val doubleExtractor: Extractor[Double] = Extractor(_.toDouble)

}

trait Extractor_1 extends Extractor_2 {
  implicit def listExtractor[T: Extractor]: Extractor[List[T]] = new Extractor[List[T]] {
    def extract(source: String): List[T] = List(implicitly[Extractor[T]].extract(source))
  }
}
trait Extractor_2 {
  implicit def generic[T]: Extractor[T] = macro Macros.generic[T, Extractor[_]]
}