Skip to content
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

override modifier not allowed for abstract vars #18692

Closed
goshacodes opened this issue Oct 13, 2023 · 4 comments
Closed

override modifier not allowed for abstract vars #18692

goshacodes opened this issue Oct 13, 2023 · 4 comments

Comments

@goshacodes
Copy link

goshacodes commented Oct 13, 2023

Compiler version

3.3.0

Minimized code

This works in scala 2, but it doesn't in scala 3

trait X {
  var y: Int
}

trait Y extends X {
  override def y_=(x: Int) = ()
  override def y = 11
}

While this one is fine in both:

trait Y extends X {
  def y_=(x: Int) = ()
  def y = 11
}

Output

error overriding variable y in trait X of type Int;
  method y of type => Int cannot override a mutable variable

Also there is a problem with concrete vars:

Minimized code

This ones returns override modifier required to override concrete member:

trait X {
  var y: Int = 12
}

trait Y extends X {
  def y_=(x: Int) = ()
}

But when I add override modifier it gives another error mutable variable cannot be overridden|

trait Y extends X {
  override def y_=(x: Int) = ()
}
@goshacodes goshacodes added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 13, 2023
@goshacodes goshacodes changed the title override modifier not allowed when implementing abstract vars override modifier not allowed for abstract vars Oct 14, 2023
@bishabosha bishabosha added area:refchecks stat:needs spec and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 14, 2023
@bishabosha
Copy link
Member

bishabosha commented Oct 14, 2023

I would assume that this should behave like in Scala 2, as the spec for 2.13 reads:

A variable declaration var x: T is equivalent to the declarations of both a getter function x and a setter function x_=:

def x: T
def x_=(y: T): Unit

An implementation of a class may define a declared variable using a variable definition, or by defining the corresponding setter and getter methods.

therefore one would assume that var desugars to ordinary methods and so can be overridden

@odersky
Copy link
Contributor

odersky commented Oct 14, 2023

Yes, but it's not trivial. Maybe someone could look how it was done in Scala 2 and re-implement it for 3?

@odersky odersky removed their assignment Oct 14, 2023
@som-snytt
Copy link
Contributor

Relatedly #13019 (where the casual tweak was reverted)

@bishabosha
Copy link
Member

lets also close this as duplicate of #13019

@bishabosha bishabosha closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants