Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extractor object generation
/**
* 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)
}
}
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
You can’t perform that action at this time.