I have a question about traits in Scala.
I have a trait returns a type of object (AA or BB) depending on the value of parameter country.
My implementattion covers the following approach:
trait firstT{
def GetType(country: String) : OType = {
//i would like to call OType.load(country)
}
}
trait OType[T <: OType[T]] {
def load(country: String): OType[T] = ???
//I would like to return AA or BB depending on country
}
class AA extends OType[AA] {
override def load(country: String): AA = new AA
}
class BB extends OType[BB] {
override def load(country: String): BB = new BB
}
Could you please help me on this? Thank you Best Regards
load
is a "static" method of the trait so it belongs in the companion object not the class definition.So the code you want might look like this
If you really need the
OType
trait to be parameterised by type then use an abstract type member: