3

Say I have a number of traits that each add some behaviour to classes that extend Actor.

They each require a small subset of the methods on Actor. Is it ok for them all to extend Actor like this?

trait A extends Actor {
  def doAThing = { ... }
}
trait B extends Actor {
  def doBThing = { ... }
}
trait C extends Actor {
  def doCThing = { ... }
}

class D extends Actor with A with B with C

Or should each trait define the methods it needs from Actor as abstract methods such that they are supplied by the final, concrete class, which is the only one that will then extend Actor?

Russell
  • 12,261
  • 4
  • 52
  • 75
  • 5
    See [What is the difference between scala self-types and trait subclasses?](http://stackoverflow.com/questions/1990948/what-is-the-difference-between-scala-self-types-and-trait-subclasses). That's probably what are you looking for. – 4e6 Mar 28 '12 at 12:02
  • I didn't know about self-types at all - if you think this question is a different enough route to the same answer, then post it and I'll accept it. – Russell Mar 28 '12 at 12:12

1 Answers1

6

Inheritance

trait A extends Actor

means that A is Actor. The other way is to use self-types

trait B {
  self: Actor =>
}

means that B requires Actor to be mixed in.

See linked question for details.

Community
  • 1
  • 1
4e6
  • 10,696
  • 4
  • 52
  • 62