I'm looking for help with the following problem:
case class A(val name: String)
class B(name: String) extends A(name)
class Base[T <: A](param: T)
class SubClass[T <: B](param: T)
object Factory {
def create[T <: A](param: T) = {
param.name match {
case "something" => new Base(param)
case "something else" => new SubClass(param)
}
}
}
The factory doesn't compile because of the mismatch between the param Subclass is expecting (T :< B) and the definition of T in create which isT :< A. Is there a clean solution for this or do I need to downcast on the call to the Subclass constructor? how would the downcast look like?
Just to be clear - when Subclass is contructed with param, param is indeed of T<: B.
Thanks.