5

For example, this:

val queue = new BasicIntQueue with Doubling with Incrementing with Filtering
queue.put(1)
println(queue.get())

will print:

Filtering
Incrementing
Doubling
put
4

As for me it would be more readable if it executed from left to right, in the order I wrote operations.

user4298319
  • 432
  • 3
  • 10
  • 2
    I'm not terribly familiar with Scala, but I'm guessing just how that code is parsed. So it's `(((new BasicIntQueue with Doubling) with Incrementing) with Filtering)`, and expressions like that are evaluated innermost-paren-first. – millimoose Jul 12 '13 at 17:25
  • Yes, see [this](http://stackoverflow.com/questions/7230808/inheriting-a-trait-twice) – S.R.I Jul 12 '13 at 17:25

4 Answers4

7

Because it follows the same pattern as inheritance. Imagine that you had something like this:

class BasicIntQueue 
class Doubling extends BasicIntQueue
class Incrementing extends Doubling
class Filtering extends Incrementing

val queue = new Filtering

You'll get the same results as you saw: Filtering gets executed first, then pass on to Incrementing, then Doubling and finally BasicIntQueue.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
6

Don't think 'executing', because it's not... think 'layering': if you add another with XYZ on the right, you've added another layer on top.

Richard Sitze
  • 8,262
  • 3
  • 36
  • 48
3

The truth is more complicated, see the Language Reference "5.1.2 Class Linearization" (page 56), or the explanation by Jim McBeath

Landei
  • 54,104
  • 13
  • 100
  • 195
0

To solve the diamond problem of multiple inharitance

Kfir Bloch
  • 68
  • 2
  • 6