diff options
Diffstat (limited to 'examples/src/main')
-rw-r--r-- | examples/src/main/scala/example.scala | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/examples/src/main/scala/example.scala b/examples/src/main/scala/example.scala index a031fbe..1e2f104 100644 --- a/examples/src/main/scala/example.scala +++ b/examples/src/main/scala/example.scala @@ -2,25 +2,36 @@ package magnolia import language.experimental.macros -trait Extractor[T] { - def extract(src: String): T +case class Thing(str: String) { + def access(path: String): Thing = Thing(s"$str.$path") +} + +trait Extractor[T] { ext => + + def extract(src: Thing): T + + def orElse[TS >: T, T2 <: TS](ext2: Extractor[T2]): Extractor[TS] = new Extractor[TS] { + def extract(src: Thing): TS = try ext.extract(src) catch { + case e: Exception => ext2.extract(src) + } + } } object Extractor extends Extractor_1 { - def apply[T](fn: String => T): Extractor[T] = new Extractor[T] { - def extract(source: String): T = fn(source) + def apply[T](fn: Thing => T): Extractor[T] = new Extractor[T] { + def extract(source: Thing): T = fn(source) } - implicit val intExtractor: Extractor[Int] = Extractor(_.toInt) - implicit val stringExtractor: Extractor[String] = Extractor(identity) - implicit val doubleExtractor: Extractor[Double] = Extractor(_.toDouble) + implicit val intExtractor: Extractor[Int] = Extractor(_.str.length) + implicit val stringExtractor: Extractor[String] = Extractor(_.str) + implicit val doubleExtractor: Extractor[Double] = Extractor(_.str.length.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)) + def extract(source: Thing): List[T] = List(implicitly[Extractor[T]].extract(source)) } } trait Extractor_2 { |