aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2017-05-29 19:05:19 -0600
committerJon Pretty <jon.pretty@propensive.com>2017-05-29 19:05:19 -0600
commita5e3bd7b949ba9b2997b1cbcca5365c1f97e2487 (patch)
treeeaf85686122cbd6d5f32eea47c5adf382e00469b /examples
parent17cfdb350fa38454a76ed5370ac6f36c3a532d3e (diff)
downloadmagnolia-a5e3bd7b949ba9b2997b1cbcca5365c1f97e2487.tar.gz
magnolia-a5e3bd7b949ba9b2997b1cbcca5365c1f97e2487.tar.bz2
magnolia-a5e3bd7b949ba9b2997b1cbcca5365c1f97e2487.zip
Pretty close to having the generic macro working
And yet so far.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/main/scala/example.scala27
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 {