dimanche 24 novembre 2019

Checking equality of types after type checking with scala.tools.reflect.ToolBox

I want to determine whether expressions represented by syntax trees tree1 and tree2 are of the same type. I tried to do so using type checking method from scala.tools.reflect.ToolBox, but it seems to be inconsistent with the actual Scala types.

Here I create the toolbox:

scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] 
              = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@1ad29bfd

It reports type inequality:

scala> tb.typecheck(tree1).tpe =:= tb.typecheck(tree2).tpe
res81: Boolean = false

I ask for the types' representation:

scala> tb.typecheck(tree1).tpe
res82: tb.u.Type = st1 with st2{val x: X; val y: Y}

scala> tb.typecheck(tree2).tpe
res83: tb.u.Type = scala.AnyRef{val x: X; val y: Y}

Now these types successfully unified:

scala> implicitly[st1 with st2{val x: X; val y: Y} =:= scala.AnyRef{val x: X; val y: Y}]
res84: =:=[st1 with st2{val x: X; val y: Y},AnyRef{val x: X; val y: Y}] = <function1>

Can I somehow check if tree1 and tree2 represent expressions of similar types as it is done in the last snippet?





Aucun commentaire:

Enregistrer un commentaire