How linearization works

In Scala, linearizations are listed from left to right where the right-most class is the most general, for example, AnyRef. While doing linearization, Any is also added to the hierarchy list. This, combined with the rule that any class must include the linearization of its superclass, means that the superclass linearization will appear as a suffix of the class linearization.

Let's see an example with some really simple classes:

class Animal extends AnyRefclass Dog extends Animal

The linearization of these two classes will be, respectively:

Animal -> AnyRef -> AnyDog -> Animal -> AnyRef -> Any

Now, let's try and formalize an algorithm that describes how a linearization is calculated:

  1. Start with the following class ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.