Mandroid
Mandroid

Reputation: 7468

Mixing a trait in a class

From 'Programming In Scala', chapter 12(on traits):

The Doubling trait has two funny things going on. The first is that it declares a superclass, IntQueue. This declaration means that the trait can only be mixed into a class that also extends IntQueue. Thus, you can mix Doubling into BasicIntQueue, but not into Rational.

But we can have our class extend any class and then mixin the trait using 'with' keyword.

Upvotes: 0

Views: 823

Answers (2)

pramesh
pramesh

Reputation: 1954

Quote this line from book:

This declaration means that the trait can only be mixed into a class that also extends IntQueue . Thus, you can mix Doubling into BasicIntQueue , but not into Rational . You can only mix in a trait with any other class that also inherits from the super-class of the mixing trait.

Upvotes: 1

Alexey Romanov
Alexey Romanov

Reputation: 170713

But we can have our class extend any class and then mixin the trait using 'with' keyword.

No, you can't. If you try it with this trait:

class X extends Rational with Doubling

you'll get an error because X would have two superclasses: Rational and IntQueue from Doubling. That's what the book says.

Upvotes: 2

Related Questions