Skip to content

Semantic bug when override var with val and no way to extends from a trait that has a var for a case class #14722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pootow opened this issue Mar 21, 2022 · 1 comment · Fixed by #14724

Comments

@pootow
Copy link

pootow commented Mar 21, 2022

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.

@pootow pootow added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 21, 2022
@pootow pootow changed the title Semantic bug when override var with val Semantic bug when override var with val and no way to extends from a trait that has a var for a case class Mar 21, 2022
@pootow pootow changed the title Semantic bug when override var with val and no way to extends from a trait that has a var for a case class Semantic bug when override var with val and no way to extends from a trait that has a var for a case class Mar 21, 2022
odersky added a commit to dotty-staging/dotty that referenced this issue Mar 21, 2022
…iables.

Extend "cannot override mutable variable" restriction also to deferred variables.
This aligns the behavior with Scala 2.

Fixes scala#14722
@odersky odersky added area:refchecks and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Mar 21, 2022
@odersky
Copy link
Contributor

odersky commented Mar 21, 2022

The erroneous behavior was introduced in #13744. Before it was a compile error.

We need to make it a compile error again unless we can fix the underlying issues. This means jumping into trait_setter logic, which is not fun.

Meanwhile, the following will work:

trait HasId:
  def id: String
  def id_=(s: String): Unit

case class Entity(var id: String) extends HasId

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants