2

Parametrized components work well with the cake pattern as long as you are only interested in a unique component for each typed component's, example:

trait AComponent[T] {
  val a:A[T]

  class A[T](implicit mf:Manifest[T]) {
    println(mf)
  }
}

class App extends AComponent[Int] {
  val a = new A[Int]()
}

new App

Now my application requires me to inject an A[Int] and an A[String], obviously scala's type system doesn't allow me to extends AComponent twice. What is the common practice in this situation ?

Nicolas
  • 277
  • 5
  • 13

1 Answers1

1

I think the AComponent doesn't need to be parameterized itself. So loose the type parameter and change this into

trait AComponent {
    val aInt: A[Int]
    val aStr: A[String]
    class A[T](implicit mf:Manifest[T]) {
        println(mf)
    }
} 

class App extends AComponent {
    val aInt = new A[Int]()
    val aStr = new A[String]()
}

if you want to be able to provide instances for Int and String

Marius Danila
  • 10,311
  • 2
  • 34
  • 37
  • It is indeed the better solution if his example isn't only illustrative and is missing some relevant part to what he intents to achieve. – pedrofurla Nov 20 '12 at 19:15
  • Thanks for your answer. This is indeed the best I've came to. The problem with this is that BComponent injecting B[T](a:A[T])'s needs to call the constructor on B with the typed arguments. (typically `val bInt = new B(aInt)`) which lowers the value of the cake pattern in this situation – Nicolas Nov 21 '12 at 09:40