I'm completely baffled by the following behavior of macros:
The problem: I'm trying to write a query engine for a model built on case classes
, where the user only has to specify the fields against which he wishes to match.
The current approach: Currently, I'm doing the lazy thing and just exploiting default Scala pattern matching, using a macro to create the appropriate match
statement. The code is the following:
// This is the macro code, defined in project `macros`
import scala.reflect.macros.whitebox
object QueryEngineMacros {
import scala.language.experimental.macros
def cimpl(c: whitebox.Context)(cs: c.Expr[CallSite], q: c.Expr[String]): c.Tree = {
import c.universe._
val pattern = cq"$q => true"
q"""$cs match {
case ${pattern}
case _ => false
}"""
}
def coincides(cs: CallSite, q: String): Boolean = macro cimpl
}
// This is the engine code, defined in project `queries` (depends on `macros`)
object QueryEngine {
def apply(q: String, res: ExtractionResult): Seq[CallSite] =
res.callSites.filter(cs => QueryEngineMacros.coincides(cs, q))
}
The error: This is my first time using Scala macros, but everything seems fine. It doesn't complain about dependencies either. However, I get this error message:
Error:(27, 66) type mismatch;
found : String
required: some.package.CallSite
res.callSites.filter(cs => QueryEngineMacros.coincides(cs, q))
Does anybody know what could be the cause of this?
EDIT: Typo fix