Scala How to Define a Method in a Trait to Return a Subclass Generically -


given simplified model of trying implement:

sealed trait data case class customerdata(values: list[string]) case class employeedata(values: list[string]) 

i want define method on trait requires each of case classes define method returns new instance additional string. following doesn't seem work , perplexes me want t not me kind of data actual type of subclass:

sealed trait data {   def withnewvalue[t <: data](v : string) : t } 

the subclass should like:

case class customerdata(values: list[string]) {   override def withnewvalue[customerdata](v : string) : customerdata = customerdata(v :: values) } 

but doesnt compile nor restrict override require type customerdata rather employeedata permitting silly like:

case class customerdata(values: list[string]) {   override def withnew[employeedata](v : string) : employeedata = employeedata(v :: values) } 

any ideas how can accomplish going for?

one way parameterize trait itself, not method:

sealed trait data[t <: data[t]] {   def withnewvalue(v : string): t }  case class customerdata(values: list[string]) extends data[customerdata] {   def withnewvalue(v : string): customerdata = customerdata(v :: values) } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -