Does the super value in laid out features depend on the site of the call?

I cannot come up with a very good description of this in words, so please take a look at this example:

trait Base { def foo = "Base" }
trait One extends Base { override def foo = "One <: " + super.foo }
trait Two extends Base { override def foo = "Two <: " + super.foo }

new Base with One with Two {} foo

Fingerprints: Two <: One <: Baseas I expect. Now I'm trying to add another level, so that to redefine the traits you do not need to explicitly call super. Like this:

trait Base { def foo = "Base" }
trait Foo extends Base { def bar = foo + " <: " + super.foo }
trait One extends Foo { override def foo = "One" }
trait Two extends Foo { override def foo = "Two" }

new Foo with One with Two {} bar

Here the last line prints Two <: Base

So this looks like the first example supermeans One, and in the last it skips Oneand goes directly to Base.

Why is this happening? Shouldn't the behavior be the same?

+4
source share
2 answers

new Base with One with Two {} foo ( new One with Two {} foo), " " . Two foo, foo (One), foo (Base).

new Foo with One with Two {} bar ( new One with Two {} bar), " " - Base- > Foo- > One- > Two. bar, Two bar One bar. foo bar, foo (Base).

UPDATE

, @Dima.

trait Base { def foo = "Base" }
trait Foo extends Base { def bar = foo + " <: " + super.foo }
trait One extends Foo { override def bar = super.bar
                        override def foo = "One" }
trait Two extends Foo { override def bar = super.bar
                        override def foo = "Two" }

new One with Two {} bar  // no Base or Foo needed

, , : res0: String = Two <: Base

Two bar (One), bar (foo), foo (not bar) .

bar foo. Two foo , One.foo .

.

trait B { def id = "B" } // B for Base

trait V extends B { override def id = "V" }
trait W extends B { override def id = "W" }
trait X extends B { override def id = "X" }
trait Y extends B { override def id = "Y" }
trait Z extends B { override def id = "Z" }

trait R extends B { override def id = "R"; def mySup = super.id } // Required

.

val r = new V with Y with W with R with X {} // B not needed
// or
val r = new W with R with Z with X with V {}
// or
val r = new R with Y with V with B with W {}
// or
val r = new Z with Y with X with W with R {}
// etc.

r.id , r.mySup , R ( B, R).

+1

super.foo Foo Base.foo Foo, . super , " ".

, . . self-types trait?

0

All Articles