Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Scala's traits execute from right to left?

Tags:

scala

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.

like image 876
user4298319 Avatar asked Jul 12 '13 17:07

user4298319


1 Answers

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.

like image 82
Daniel C. Sobral Avatar answered Oct 11 '22 14:10

Daniel C. Sobral