Mixing multiple traits in Scala

后端 未结 2 2012
孤街浪徒
孤街浪徒 2021-01-31 07:12

Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types.

Suppose I have the traits Student, Worker, Underpaid, and Youn

2条回答
  •  Happy的楠姐
    2021-01-31 08:02

    I think that it is very important to explain not only the syntax, but also which role does the ordering of the traits play. I found the explanation in Jason Swartz's Learning Scala (page 177) quite enlightning.

    • A Scala class can extend multiple traits at once, but JVM classes can extend only one parent class. The Scala compiler solves this by creating "copies of each trait to form a tall, single-column hierarchy of the class and traits", a process known as linearization.

    • In this context, extending multiple traits with identical field names would fail to compile, exactly the same "as if you were extending a class and providing your own version of a method but failed to add an override keyword".

    And since it determines the shape of the inheritance tree, the linearization order is indeed one very important question to regard. As an example, class D extends A with B with C (where A is a class and B and C are traits) would become class D extends C extends B extends A. The following few lines, also from the book, illustrate that perfectly:

    trait Base { override def toString = "Base" }
    class A extends Base { override def toString = "A->" + super.toString }
    trait B extends Base { override def toString = "B->" + super.toString }
    trait C extends Base { override def toString = "C->" + super.toString }
    class D extends A with B with C { override def toString = "D->" + super.toString }
    

    A call to new D() would have the REPL print the following:

     D->C->B->A->Base
    

    Which perfectly reflects the structure of the linearized inheritance graph.

提交回复
热议问题