Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handy ways to show linearization of a class?

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 case, I'm trying to understand the linearization of ArrayBuffer instances.
(I'm trying to make a similar class that's a Buffer and an IndexedSeqOptimized, and I'm finding complaints from the compiler that overriding method seq has incompatible type)

The rules for linearization of scala classes are described in the scala spec section 5.1.2. As I understand it, method overrides go in linearization order, and initialization goes in reverse linearization order.

like image 628
Lee Mighdoll Avatar asked Mar 25 '13 19:03

Lee Mighdoll


1 Answers

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
like image 158
ghik Avatar answered Sep 17 '22 10:09

ghik