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

Confusing behaviour with abstract var and override #13019

Open
oyvindberg opened this issue Jul 5, 2021 · 3 comments · Fixed by #13744
Open

Confusing behaviour with abstract var and override #13019

oyvindberg opened this issue Jul 5, 2021 · 3 comments · Fixed by #13744

Comments

@oyvindberg
Copy link

Compiler version

3.0.0

Minimized code

// no `override` - fine
trait Ok1 { var i: Int }
class Ok1C extends Ok1 { var i: Int = 1 } 

// `override` of desugared `var` - fine
trait Ok2 {
  def i: Int
  def i_=(v: Int): Unit
}
class Ok2C extends Ok2 { override var i: Int = 1 }

// fails
trait NotOk {var i: Int}
class NotOkC extends NotOk { override var i: Int = 1 }

Output

error overriding variable i in trait NotOk of type Int;
  variable i of type Int cannot override a mutable variable

Expectation

override is used to implement abstract or re-implement concrete things, and guarantees their presence. In this case it seems impossible to use override to implement an abstract var.

The example compiles in scala 2

@som-snytt
Copy link
Contributor

I submitted a quickie PR. override of abstract var is fine.

@odersky
Copy link
Contributor

odersky commented Mar 21, 2022

The fix in #13744 was not semantically correct.

When did Scala-2's behavior change? I tried with 2.13.5 and it also flags overrides of variables as errors.

@smarter
Copy link
Member

smarter commented Mar 21, 2022

When did Scala-2's behavior change?

In scala/scala@9b59f5f which is part of 2.12.0. I tried it in both 2.12.0 and 2.13.5 and could get the following code to compile:

trait NotOk { var i: Int }
class NotOkC extends NotOk { override var i: Int = 1 }

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.

6 participants