Scala Quasikote宏示例断开类型签名关闭
我从书中选取了这个Scala Quasikote示例 我得到这个错误: 类型推断显示“Trees#Tree”,但类型推断已关闭Scala Quasikote宏示例断开类型签名关闭,scala,intellij-idea,macros,scala-macros,scala-quasiquotes,Scala,Intellij Idea,Macros,Scala Macros,Scala Quasiquotes,我从书中选取了这个Scala Quasikote示例 我得到这个错误: 类型推断显示“Trees#Tree”,但类型推断已关闭 import scala.reflect.api.Trees // For Trees#Tree (TreeNode) import scala.reflect.macros.blackbox._ import scala.reflect.runtime.universe._ // To use Scala runtime reflection /** * Rep
import scala.reflect.api.Trees // For Trees#Tree (TreeNode)
import scala.reflect.macros.blackbox._
import scala.reflect.runtime.universe._ // To use Scala runtime reflection
/**
* Represents a macro invariant which is checked over the corresponding statements.
* Example:
* '''
* var mustBeHello = "Hello"
* invariant.execute(mustBeHello.equals("Hello")) {
* mustBeHello = "Goodbye"
* }
* // Throws invariant.InvariantFailure
* '''
*/
object invariant {
case class InvariantFailure(message: String) extends RuntimeException(message)
type SyntaxTree = scala.reflect.runtime.universe.Tree
type TreeNode = Trees#Tree // a syntax tree node that is in and of itself a tree
// These two methods are the same, but one is a function call and the other is a macro function call
def execute[RetType] (myPredicate: => Boolean)(block: => RetType): RetType = macro executeMacro
def executeMacro(context: Context)(myPredicate: SyntaxTree)(block: SyntaxTree) = {
val predicateString: String = showCode(myPredicate) // turn this predicate into a String
val q"..$statements" = block // make the block into a sequence of statements
val myStatements: Seq[TreeNode] = statements // the statements are a sequence of SyntaxTreeNodes, each node a little Tree
val invariantStatements = statements.flatMap { statement =>
// Error here:
val statementString: String = showCode(statement) /* Type mismatch, expected Tree, actual Trees#Tree */
val message: String =
s"FAILURE! $predicateString == false, for statement: " + statementString
val tif: SyntaxTree =
q"throw new metaprogramming.invariant.InvariantFailure($message)"
val predicate2: SyntaxTree =
q"if (false == $myPredicate) $tif"
val toReturn: List[SyntaxTree] =
List(q"{ val temp = $myStatements; $predicate2; temp };")
toReturn
}
val tif: SyntaxTree =
q"throw new metaprogramming.invariant.InvariantFailure($predicateString)"
val predicate: SyntaxTree =
q"if (false == $predicate) $tif"
val toReturn: SyntaxTree =
q"$predicate; ..$invariantStatements"
toReturn
}
}
^文件应该是不言自明的。类型推断为Tree#Tree,但在示例代码中添加“:Tree#Tree”会导致编译失败,并出现错误:
[info] Compiling 2 Scala sources to /home/johnreed/sbtProjects/scala-trace-debug/target/scala-2.11/test-classes...
[error] /home/johnreed/sbtProjects/scala-trace-debug/src/test/scala/mataprogramming/invariant2.scala:30: type mismatch;
[error] found : TreeNode
error scala.reflect.api.Trees#Tree
[error] required: context.universe.Tree
[error] val exceptionMessage = s"FAILURE! $predicateAsString == false, for statement: " + showCode(statement)
我要进IntelliJ了
[info] Compiling 2 Scala sources to /home/johnreed/sbtProjects/scala-trace-debug/target/scala-2.11/test-classes...
[error] /home/johnreed/sbtProjects/scala-trace-debug/src/test/scala/mataprogramming/invariant2.scala:30: type mismatch;
[error] found : TreeNode
error scala.reflect.api.Trees#Tree
[error] required: context.universe.Tree
[error] val exceptionMessage = s"FAILURE! $predicateAsString == false, for statement: " + showCode(statement)
这些类型真的很时髦。要么IntelliJ搞乱了类型,要么我不理解这个概念。发现推断类型的“正常”方法是:
- 使用
:键入
- 分配到错误类型并观察错误消息
- 调用一个函数,该函数显示事物的
类型标签
[error] /home/apm/clones/prog-scala-2nd-ed-code-examples/src/main/scala/progscala2/metaprogramming/invariant2.scala:25: type mismatch;
[error] found : List[context.universe.Tree]
[error] required: Int
[error] val foo: Int = statements
[error] ^
这表明树
在上下文中是路径依赖的
你不能用任何一棵老树来喂它
类似问题出现在。代码来自何处?它不在回购协议中。这些类型是路径相关的,这就是为什么要使用
c.Tree
等。repo会编译。@som snytt-这段代码有些修改。来自repo的未修改代码将编译,但类型已关闭。看,你的代码不应该编译。当你指a.B.时,你不能使用类型项目a#B。我试试你的票证代码。