handy ways to show linearization of a class?

十年热恋 提交于 2019-11-30 14:11:03

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!