我将使用shapeless库作为示例:
import shapeless.test.illTyped假设我想将illType函数包装在另一个函数中,我尝试了两种不同的方法:
(按值)
def shouldTypeError(v: String) = illTyped(v, ".*")
[ERROR] ... : exception during macro expansion:
scala.MatchError: v (of class scala.reflect.internal.Trees$Ident)
at shapeless.test.IllTypedMacros.applyImpl(typechecking.scala:43)(按名称)
def shouldTypeError(v: => String) = illTyped(v, ".*")
[ERROR ... ]: exception during macro expansion:
scala.MatchError: v (of class scala.reflect.internal.Trees$Ident)
at shapeless.test.IllTypedMacros.applyImpl(typechecking.scala:43)因此,它们都没有按预期工作。这在最新的scala或dotty中是可能的吗?
发布于 2021-06-20 20:03:45
不是的。外部函数也必须是宏
def shouldTypeError(v: String): Unit = macro shouldTypeErrorImpl
def shouldTypeErrorImpl(c: blackbox.Context)(v: c.Tree): c.Tree = {
import c.universe._
q"""_root_.shapeless.test.illTyped($v, ".*")"""
}https://stackoverflow.com/questions/67526001
复制相似问题