Scala reflection: knownDirectSubclasses only works for sealed tags?

Given this issue and SI-7046 , this is not all that I expected.

scalac test.scala && scala Testin Scala 2.11.6, the following displays empty Set():

trait Foo
case class Bar() extends Foo
case class Baz() extends Foo

object Test {
  def main(args: Array[String]) {
    import scala.reflect.runtime.universe._
    println( typeOf[Foo].typeSymbol.asClass.knownDirectSubclasses )
  }
}

However, if I change trait Footo sealed trait Foo, it prints Set(class Bar, class Baz)as expected.

What's going on here?

+4
source share
1 answer

view document

/** If this is a sealed class, its known direct subclasses.
 *  Otherwise, the empty set.
 *
 *  @group Class
 */
def knownDirectSubclasses: Set[Symbol]
+1
source

Source: https://habr.com/ru/post/1618467/


All Articles