I'm trying to create a match statement using macros, that matches all subclasses of a given type. But I have trouble with extracting the field values of the case classes. For example:
sealed abstract class Foobar
case class Foo(x:Int,f:Foobar) extends Foobar
case class Bar(s:String, f:Foobar) extends Foobar
Now I want to create code that looks like this, when Foobar is given:
e1 match {
case Foo(args) => args.toString
case Bar(args) => args.toString
}
Thats what I got so far:
def eqImpl[A: c.WeakTypeTag](c: Context)(e1: c.Expr[A], e2: c.Expr[A]): c.Expr[Boolean] = {
import c.universe._
val tpe = c.weakTypeOf[A].typeSymbol.asClass
tpe.typeSignature // SI-7046
val subclasses = tpe.knownDirectSubclasses
val cases =
subclasses.map{ clazz =>
cq"x: $clazz => x "
}
println(cases)
reify(true)
}
This code would match Foo and Bar, but I can not extract the fields, which I need on the right hand side.