Closed
Description
Compiler version
3.1.1
Minimized code
trait HasId(var id: String)
case class Entity(override val id: String) extends HasId(id)
val entity = Entity("0001")
entity.id = "0002"
println(entity.id)
Output
0001
Expectation
Either does not compile or output 0002
.
My intention is that I do not want to introduce a redundant variable just for assigning to trait parameter.
trait HasId(var id: String)
case class Entity(var idJustForAssignment: String) extends HasId(idJustForAssignment)
val entity = Entity("id")
entity.id
entity.idJustForAssignment // this is a duplication
and this does not work:
trait HasId(var id: String)
case class Entity(id: String) extends HasId(id) // and this works when it's a normal class not a case class
and this does not work either:
trait HasId(var id: String)
case class Entity(override var id: String) extends HasId(id)
So there's no way to extends from a trait that has a var
for a case class.