Skip to content

Instantly share code, notes, and snippets.

@gkossakowski
Created November 12, 2012 18:49
Show Gist options
  • Save gkossakowski/4061123 to your computer and use it in GitHub Desktop.
Save gkossakowski/4061123 to your computer and use it in GitHub Desktop.
RefinedType accessible through reflection and having different owner depending when the symbol comes from (unpickling vs type-checking)
$ cat Foo.scala
object test3 {
trait A; trait C
trait B { self: A with C =>
class Inner {
def b = B.this
}
}
}
$ ./bin/scalac Foo.scala -d sandbox/
$ ./bin/scala -cp sandbox/
Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_10-ea).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :silent
Switched off result printing.
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => cm}
import scala.tools.reflect.ToolBox
val toolbox = cm.mkToolBox()
val src = """object test3 {
trait A; trait C
trait B { self: A with C =>
class Inner {
def b = B.this
}
}
}
"""
val tree = toolbox.parse(src)
val typedTree = toolbox.typeCheck(tree)
val Btpe = typedTree.symbol.asModule.moduleClass.asType.toType.member(newTypeName("B")).asType.toType
val bMethod = Btpe.member(newTypeName("Inner")).asType.toType.member(newTermName("b")).asMethod
val returnTpe = bMethod.returnType
val returnTpeUnpickled = typeOf[test3.B].member(newTypeName("Inner")).asType.toType.member(newTermName("b")).asMethod.returnType
// Exiting paste mode, now interpreting.
scala> :silent
Switched on result printing.
scala> returnTpe
res0: toolbox.u.Type = <expression-owner>.test3.B with <expression-owner>.test3.A with <expression-owner>.test3.C
scala> returnTpe.typeSymbol.owner
res1: toolbox.u.Symbol = <none>
scala> returnTpe.typeSymbol.owner == NoSymbol
res2: Boolean = true
scala> returnTpeUnpickled
res3: reflect.runtime.universe.Type = test3.B with test3.A with test3.C
scala> returnTpeUnpickled.typeSymbol.owner
res4: reflect.runtime.universe.Symbol = object test3
scala> returnTpeUnpickled.typeSymbol.owner == NoSymbol
res5: Boolean = false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment