Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-7567

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

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: Scala 2.10.2-RC2
    • Fix Version/s: None
    • Component/s: Reflection
    • Labels:
      None
    • Environment:

      Scala 2.10.0 through 2.10.2

      Description

      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.

        Attachments

          Activity

            People

            • Assignee:
              burmako Eugene Burmako
              Reporter:
              sciss Sciss
            • Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: