未找到Scala:值宏 [英] Scala not found: value macro
本文介绍了未找到Scala:值宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此行:
"foo".assertNonFatal( _ equals "foo", "error message");
此错误:
Error:(120, 47) not found: value assertion
final implicit class ImplicitAssert[MyType](val me: MyType) {
final def assertNonFatal(assertion: (MyType) => Boolean, message: String)
: MyType = macro ImplicitAssertMacros.assertNonFatal[MyType]
}
此宏:
object ImplicitAssertMacros {
def assertNonFatal[MyType](c: scala.reflect.macros.blackbox.Context)
(assertion: c.Tree, message: c.Tree): c.Tree = {
import c.universe._
val me = q"${c.prefix}.me" // "me" from ImplicitAssert class.
val numLines = q"Int.MaxValue"
val useStdOut = q"false"
val assertionTrue = q"assertion($me)"
val isFatal = q"false"
val toReturn = q"""
_root_.info.collaboration_station.debug.internal.Printer.traceInternalAssert($message, $numLines, $useStdOut, $assertionTrue, $isFatal);
$me;
"""
toReturn
}
}
^我不知道这个编译器错误的原因是什么或如何解决它。
^ I can't figure out what the cause of this compiler error is or how to fix it.
请注意,只有在编译源文件而不是宏时才会发生。
Note that it only happens when I compile the source file, not the macros.
推荐答案
它的工作原理是:
val assertionTrue = q"$assertion($me)"
def宏会在调用网站展开,其中不再有任何断言参数。
A def macro expands at the call site, where there is no longer any assertion parameter.
这篇关于未找到Scala:值宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文