Created
June 14, 2014 19:24
-
-
Save Arneball/5b7e2e90d889ef9d30ed to your computer and use it in GitHub Desktop.
Extractor object generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by arneball on 2014-06-14. | |
*/ | |
object ExtractorTest extends App with Integral.ExtraImplicits{ | |
@extract def longString(str: String) = str.length > 1 | |
@extract def cool(int: Int) = Option(int).filter{ 2 < _ }.map{ 2 * } | |
@extract def evensum[T : Integral](l: List[T]) = { | |
l.sum % implicitly[Integral[T]].fromInt(2) == 0 | |
} | |
List(2, 2, 2) match { | |
case evensum() => println("even sum! :) ") | |
} | |
"" match { | |
case longString() => println("hurray") | |
case _ => println("fail") | |
} | |
3 match { | |
case cool(that) => println(that) | |
case that => println(that) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.StaticAnnotation | |
import scala.reflect.macros.blackbox.Context | |
import language.experimental.macros | |
/** | |
* Created by arneball on 2014-06-14. | |
*/ | |
class extract extends StaticAnnotation { | |
def macroTransform(annottees: Any*) = macro ex.impl | |
} | |
object ex { | |
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { | |
import c.universe._ | |
annottees.head.tree match { | |
case q"def $name[..$tp](...$param): $ret = $body" => | |
val (h::r1) :: r2 = param | |
c.Expr[Any]{ | |
q""" | |
object $name { | |
def unapply[..$tp]($h)(...${r1 :: r2}): $ret = $body | |
} | |
""" | |
} | |
case _ => c.abort(c.enclosingPosition, "must be a method with 1 parameter") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment