Eug*_*ota 5 scala path-dependent-type scala-macros
我如何解决两个与路径相关的类型的等价,我知道它们是相同但编译器没有?
使用Scala 2.10.0 M7我试图将AST从一个宇宙转换为另一个宇宙.
case class MacroBridge(context: Context) {
def toMacroTree(tree: treehugger.forest.Tree): context.universe.Tree = ???
def fromMacroTree(tree: context.universe.Tree): treehugger.forest.Tree = ???
}
Run Code Online (Sandbox Code Playgroud)
在宏实现中,我可以将其用作:
val bridge = treehugger.MacroBridge(c)
def fromMacroTree(tree: c.universe.Tree): Tree = bridge.fromMacroTree(tree)
Run Code Online (Sandbox Code Playgroud)
但是,这会导致编译器错误:
[error] /scalamacros-getting-started/library/Macros.scala:21: type mismatch;
[error] found : c.universe.Tree
[error] required: bridge.context.universe.Tree
[error] possible cause: missing arguments for method or constructor
[error] def fromMacroTree(tree: c.universe.Tree): Tree = bridge.fromMacroTree(tree)
Run Code Online (Sandbox Code Playgroud)
在上面的代码c显然是相同的值bridge.context,但也许是因为它是一个值类型检查器无法检查它.放置通用类型约束没有帮助:
def fromMacroTree[A](tree: A)(implicit ev: A =:= context.universe.Tree): Tree =
Run Code Online (Sandbox Code Playgroud)
在宏中,这仍然导致错误:
[error] /scalamacros-getting-started/library/Macros.scala:21: Cannot prove that c.universe.Tree =:= bridge.context.universe.Tree.
[error] def fromMacroTree(tree: c.universe.Tree): Tree = bridge.fromMacroTree(tree)
Run Code Online (Sandbox Code Playgroud)
我需要访问权限,context.universe所以我可以访问其他依赖类型TermName.除了演员之外还有更好的解决方法吗?:
def fromMacroTree(tree: c.universe.Tree): Tree =
bridge.fromMacroTree(tree.asInstanceOf[bridge.context.universe.Tree])
Run Code Online (Sandbox Code Playgroud)
我可以做以下工作:
case class MacroBridge[C <: Context](context: C) {
def fromMacroTree(tree: context.universe.Tree): context.universe.Tree = ???
}
trait MB {
def meth(c: Context) {
val bridge = MacroBridge[c.type](c)
def fromMacroTree(tree: c.universe.Tree): c.universe.Tree =
bridge.fromMacroTree(tree)
}
}
Run Code Online (Sandbox Code Playgroud)
前段时间我遇到了几乎相同的问题.