I would like to rewrite partial function
into match
expression.
//macro and impl
def pfRewrite(pf: PartialFunction[Int, Int]) = macro ResponderImpl.pfRewriteImpl
def pfRewriteImpl(c: Context)(pf: c.Expr[PartialFunction[Int, Int]]) = {
import c.universe._
val cas = pf.tree.collect {
case x: DefDef if x.name.decoded == "applyOrElse" =>
x.collect {
case cq"$x => $y" => cq"""$x => $y"""
}
}.flatten
val sVal = newTermName("someVal")
val rewrite = q"""
$sVal match {
case ..$cas
}
"""
c.Expr(rewrite)
}
In code i got a PartialFunction
and take a cases
from applyOrElse
method, next i create a match
expression for "someVal". This value from code:
//test
def test {
val someVal = 10
pfRewrite {
case 1 => 10
case _ => 100
}
}
But i got errors:
[error] found : Int(10)
[error] required: Nothing
[error] case 1 => 10
[error] ^
and etc.
It possible rewrite partial function call into match ?