Scala: equivalence of path dependent types

How do I deal with the equivalence of two path-dependent types, which, as I know, are the same, but the compiler does not work?

Using Scala 2.10.0 M7 I am trying to convert an AST from one universe to another.

case class MacroBridge(context: Context) {
  def toMacroTree(tree: treehugger.forest.Tree): context.universe.Tree = ???
  def fromMacroTree(tree: context.universe.Tree): treehugger.forest.Tree = ???
}

As part of the macro implementation, I can use it as:

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

However, this leads 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)

The code above is cclearly the same value as bridge.context, but perhaps because it cannot verify it. Applying a generic type constraint did not help:

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

In the macro, this still led 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 access to context.universe, so I can switch to other dependent types, for example TermName. Is there a better job than casting ?:

def fromMacroTree(tree: c.universe.Tree): Tree =
  bridge.fromMacroTree(tree.asInstanceOf[bridge.context.universe.Tree])
+5
source share
1

:

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

.

+9

All Articles