0

この Scala quasiquote の例は、本「Programming Scala」(第 2 版)から引用しました。

このエラーが発生しています: https://issues.scala-lang.org/browse/SI-9711

型推論は「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

/**
  * 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で「型の不一致、予想されるツリー、実際の Trees#Tree」が表示されます

4

2 に答える 2

0
[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 が型を台無しにしているか、概念が私を逃れています。

于 2016-03-20T21:28:05.020 に答える