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

runtime/compile time reflection doesn't see all knownDirectSubclasses

    Details

      Description

      Unlike, say, annotations or flags, knownDirectSubclasses doesn't get auto-populated and requires a symbol to be pre-initialized to work correctly.

      import scala.reflect.runtime.universe._
      import scala.reflect.runtime.{currentMirror => cm}
       
      sealed class C
      class D extends C
      class E extends C
       
      object Test extends App {
        val c = cm.staticClass("C")
        println(c.knownDirectSubclasses)
        c.typeSignature
        println(c.knownDirectSubclasses)
      }
      

      19:43 ~/Projects/Kepler_7046/sandbox (ticket/7046)$ scalac Test.scala && scala Test
      Set()
      Set(class D, class E)
      

        Attachments

          Issue Links

            Activity

            Hide
            milessabin Miles Sabin added a comment -
            Show
            milessabin Miles Sabin added a comment - WIP PR here: https://github.com/scala/scala/pull/5284
            Hide
            cchantep C. Chantepie added a comment -

            Could the following be used as workaround in some case (seems to work in my case as far as I can see).

            val cls: ClassSymbol = sub.asClass
             
            println(s"knownDirectSubclasses = ${cls.knownDirectSubclasses}")
            // print "knownDirectSubclasses = Set()"
             
            val subsub = cls.owner.typeSignature.decls.filter {
              case c: ClassSymbol =>
                cls != c && c.selfType.baseClasses.contains(cls)
             
              case _ => false
            }
             
            println(s"subsub = $subsub")
            // print the actual sub classes
            

            Show
            cchantep C. Chantepie added a comment - Could the following be used as workaround in some case (seems to work in my case as far as I can see). val cls: ClassSymbol = sub.asClass   println(s "knownDirectSubclasses = ${cls.knownDirectSubclasses}" ) // print "knownDirectSubclasses = Set()"   val subsub = cls.owner.typeSignature.decls.filter { case c: ClassSymbol => cls != c && c.selfType.baseClasses.contains(cls)   case _ => false }   println(s "subsub = $subsub" ) // print the actual sub classes
            Hide
            milessabin Miles Sabin added a comment -

            Fix backported to 2.11.8 is available here.

            Show
            milessabin Miles Sabin added a comment - Fix backported to 2.11.8 is available here .
            Show
            moors Adriaan Moors added a comment - https://github.com/scala/scala/pull/5345
            Hide
            moors Adriaan Moors added a comment -

            To track future refinements, please open a new issue. Closing this one to mark our progress.

            Show
            moors Adriaan Moors added a comment - To track future refinements, please open a new issue. Closing this one to mark our progress.

              People

              • Assignee:
                milessabin Miles Sabin
                Reporter:
                burmako Eugene Burmako
              • Votes:
                8 Vote for this issue
                Watchers:
                23 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: