Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reflection on inner classes fails when outer object is not fully reflected before #7567

Closed
scabug opened this issue Jun 10, 2013 · 4 comments

Comments

@scabug
Copy link

scabug commented Jun 10, 2013

In the following example, I am trying to get the instance of Bar (copy from Stackoverflow - http://stackoverflow.com/questions/17012294/recovering-a-singleton-instance-via-reflection-from-sealed-super-trait-when-typ/17016268#17016268 ):

import reflect.runtime.universe._
import reflect.runtime.{currentMirror => cm}

object Foo {
  case object Bar extends Foo
}
sealed trait Foo

def getModule(tpe: Type): Any = {
  val classSymbol  = tpe.typeSymbol.asClass
  val compSymbol   = classSymbol.companionSymbol  // gives <none> !
  val moduleSymbol = compSymbol.asModule
  val moduleMirror = cm.reflectModule(moduleSymbol)
  moduleMirror.instance
}

val fooC     = typeOf[Foo].typeSymbol.asClass
val subs     = fooC.knownDirectSubclasses
// fooC.companionSymbol.typeSignature
val tpeOther = subs.last.asType.toType

val res = getModule(tpeOther)
println(res)

The companion symbol resolution in the getModule method fails here. If I uncomment the line fooC.companionSymbol.typeSignature, it works as expected.

@scabug
Copy link
Author

scabug commented Jun 10, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7567?orig=1
Reporter: @Sciss
Affected Versions: 2.10.2-RC2

@scabug
Copy link
Author

scabug commented Sep 15, 2013

Lars Hupel (larsrh) said:
Related to: milessabin/shapeless#48

import scala.language.experimental.macros
import scala.reflect.macros.Context
def fooImpl[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = { println(c.weakTypeOf[T].typeSymbol.asClass.companionSymbol); c.universe.reify {} }
def foo[T] = macro fooImpl[T]

scala> { case class Frob(x: Int); foo[Frob] }
<none>

However:

import scala.reflect.runtime.universe._

scala> { case class Frob(x: Int); weakTypeOf[Frob].typeSymbol.asClass.companionSymbol }
scala.ScalaReflectionException: free type Frob is not a class

So, run-time and compile-time reflection disagree about whether Frob is a "class" or a "free type".

@scabug
Copy link
Author

scabug commented Sep 15, 2013

@xeno-by said:
That's a known issue caused by the fact that metadata of local classes is not pickled. Therefore local classes, when used in reify and weakTypeOf, are reified as dummy free types.

@SethTisue
Copy link
Member

not clear to me there's anything actionable here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants