0

When using the cake pattern, when would you want to use the self-type annotation eg:

trait DefaultFoo extends Foo {
  this: Bar =>
  ...
}

vs a stub def

trait DefaultFoo extends Foo {
  def bar:Bar
  ...
}

or a stub val

trait DefaultFoo extends Foo {
  val bar:Bar
  ...
}

Update: I'll be a little more specific (hopefully). All three of the forms define the need for a Bar instance. So basically, any non-abstract class mixing in this trait in any one of its forms must provide an implementation of Bar. My question is, when would I want to force an implementation of Bar using the self-type annotation; when would I want to force and implementation of Bar using the def stub and when would I want to force it using the val stub. I hope that now the question is a bit clearer.

Thanks Netta

netta
  • 508
  • 6
  • 16
  • typically youd do: trait DefaultFoo extends Foo { this: Bar with Foo => ... } to make sure youre trait is implementing another otherwise youd get a self illegal type inheritance error at compile time. in the 2nd example bar would be a method signature and in the 3 . a val that is a different cattle of fish – Stefan Kunze Dec 14 '13 at 21:26
  • My duplication-question action notwithstanding, cake is easier when `Bar` also depends on `Foo`. With composition, you would have something awkward as `new Foo { thisFoo => val bar = new Bar { val foo = thisFoo }}` – 0__ Dec 14 '13 at 21:37

0 Answers0