handy ways to show linearization of a class?
Asked Answered
F

1

6

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.

Flossi answered 25/3, 2013 at 19:51 Comment(0)
B
7

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
Bilestone answered 25/3, 2013 at 20:54 Comment(9)
I think the documentation is wrong though. That's linearization order - not reversed.Flossi
Submitted PR #2320 to fix the doc.Flossi
Note that you can call e.g. ru.typeOf[Integer].baseClasses to save a couple of linesFlossi
Also, we should note for future readers how to get to singleton types as well. I think this does it: ru.typeOf[List.type].baseClassesFlossi
@Bilestone would you (review and) make those edits for the benefit of future readers? Then I'll click accept!Flossi
I see there's also repl power mode: power.typeOf, which takes a string.. I wonder how to use it.Flossi
@LeeMighdoll thanks for noticing this. About power.typeOf - it seems to return type of an expression passed as a string (so this involves parsing and typechecking that expression).Bilestone
@Bilestone thanks for the edits. Clicking accept. Keep an eye out for clarification of the linearization order issue.Flossi
Any idea how to do this in Scala 3? From what I can tell, universe has been discontinued in that version.Self

© 2022 - 2024 — McMap. All rights reserved.