handy ways to show linearization of a class?

后端 未结 1 1557
太阳男子
太阳男子 2021-01-02 23:46

I\'ll bet there are some snazzy ways to show linearization in the repl via the new reflection libraries and/or repl power mode. What are they?

In my particular ca

相关标签:
1条回答
  • 2021-01-03 00:40

    There is a method baseClasses on Type and ClassSymbol in Scala reflection API which, according to its scaladoc, returns:

    The list of all base classes of this type (including its own typeSymbol) in reverse linearization order, starting with the class itself and ending in class Any.

    How to use this? Here's an example that prints all superclasses of scala.collection.immutable.List in reverse linearization order:

    import scala.reflect.runtime.universe._
    val tpe = typeOf[scala.collection.immutable.List[_]]
    tpe.baseClasses foreach { s => println(s.fullName) }
    

    Output of this:

    scala.collection.immutable.List
    scala.collection.LinearSeqOptimized
    scala.Product
    scala.collection.immutable.LinearSeq
    scala.collection.LinearSeq
    scala.collection.LinearSeqLike
    scala.collection.immutable.Seq
    scala.collection.immutable.Iterable
    scala.collection.immutable.Traversable
    scala.Immutable
    scala.collection.AbstractSeq
    scala.collection.Seq
    scala.collection.SeqLike
    scala.collection.GenSeq
    scala.collection.GenSeqLike
    scala.PartialFunction
    scala.Function1
    scala.collection.AbstractIterable
    scala.collection.Iterable
    scala.collection.IterableLike
    scala.Equals
    scala.collection.GenIterable
    scala.collection.GenIterableLike
    scala.collection.AbstractTraversable
    scala.collection.Traversable
    scala.collection.GenTraversable
    scala.collection.generic.GenericTraversableTemplate
    scala.collection.TraversableLike
    scala.collection.GenTraversableLike
    scala.collection.Parallelizable
    scala.collection.TraversableOnce
    scala.collection.GenTraversableOnce
    scala.collection.generic.FilterMonadic
    scala.collection.generic.HasNewBuilder
    java.lang.Object
    scala.Any
    
    0 讨论(0)
提交回复
热议问题