简体   繁体   English

Scala:路径依赖类型的等价性

[英]Scala: Equivalence of path-dependent types

How do I get around with equivalence of two path-dependent types that I know are the same but the compiler does not? 我如何解决两个与路径相关的类型的等价,我知道它们是相同但编译器没有?

Using Scala 2.10.0 M7 I am trying to convert an AST from one universe to another. 使用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 = ???
}

Within a macro implementation, I can use it as: 在宏实现中,我可以将其用作:

val bridge = treehugger.MacroBridge(c)
def fromMacroTree(tree: c.universe.Tree): Tree = bridge.fromMacroTree(tree)

However, this results to a compiler error: 但是,这会导致编译器错误:

[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)

In the above code c is clearly the same value as bridge.context , but maybe because it's a value type checker cannot check it. 在上面的代码中, c显然与bridge.context值相同,但也许是因为它是值类型检查器无法检查它。 Putting generalized type constraint did not help: 放置通用类型约束没有帮助:

def fromMacroTree[A](tree: A)(implicit ev: A =:= context.universe.Tree): Tree =

In the macro this still resulted to an error: 在宏中,这仍然导致错误:

[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)

I need the access to context.universe so I can get to other dependent types like TermName . 我需要访问context.universe所以我可以访问其他依赖类型,如TermName Is there a better work around besides casting?: 除了演员之外还有更好的解决方法吗?:

def fromMacroTree(tree: c.universe.Tree): Tree =
  bridge.fromMacroTree(tree.asInstanceOf[bridge.context.universe.Tree])

I could the following make to work: 我可以做以下工作:

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)
  }
}

I had nearly the same problem some time ago. 前段时间我遇到了几乎相同的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM