我正在使用 2.10.0-M5 使用 Scala 宏,但我无法弄清楚为什么编译器认为返回类型是Any
而不是List[Int]
. 如果我删除对 map 的调用并只返回列表(将宏的最后一行更改为c.Expr(list)
),它会按预期工作。此外,宏确实返回 a List[Int]
,编译器只是不知道它。
宏定义:
def test(s:String) = macro testImpl
def testImpl(c:Context)(s:c.Expr[String]):c.Expr[Any] = {
import c.universe._
val list = reify(List(1)).tree
val function = reify((x:Int) => x).tree
val res =
Apply(
Select(
list,
newTermName("map")),
List(function)
)
c.Expr(res)
}
宏调用:
val list:List[Int] = test("")
错误信息:
[error] found : Any
[error] required: List[Int]
[error] val list:List[Int] = test("")
[error] ^