Skip to content

Singleton types should not be inferred for final vars #1285

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
smarter opened this issue May 29, 2016 · 7 comments
Closed

Singleton types should not be inferred for final vars #1285

smarter opened this issue May 29, 2016 · 7 comments

Comments

@smarter
Copy link
Member

smarter commented May 29, 2016

class Test {
  final var x = false
  x = true
}
error: type mismatch:
 found   : Boolean(true)
 required: Boolean(false)
  x = true
      ^
@sjrd
Copy link
Member

sjrd commented May 29, 2016

So there is such a thing as a final var ... 😧

@DarkDimius
Copy link
Contributor

DarkDimius commented May 29, 2016

What does final var mean in the first place? Aren't final and var contradicting?

@smarter
Copy link
Member Author

smarter commented May 29, 2016

Good question, I just found it in the wild in https://github.com/molecule-labs/molecule/search?utf8=%E2%9C%93&q=%22final+var%22 but didn't stop to think if it made any sense :). http://stackoverflow.com/questions/18222233/what-is-the-usage-of-a-final-var-in-scala says it's to prevent overriding.

@DarkDimius
Copy link
Contributor

DarkDimius commented May 29, 2016

says it's to prevent overriding.

Mutable variables can't be overridden even if they are not final:

scala> class A{var s = 3}; class B extends A{override var s = 4}
<console>:10: error: overriding variable s in class A of type Int;
 variable s cannot override a mutable variable
       class A{var s = 3}; class B extends A{ override var s = 4}

@smarter
Copy link
Member Author

smarter commented May 29, 2016

@adriaanm: any idea if the semantics of final var are different from the semantics of var ?

@adriaanm
Copy link
Contributor

adriaanm commented May 30, 2016

As @DarkDimius said, currently, final is implied for a var. I can't think of any difference implied by the presence of the modifier. I guess you could defend a design where you can override a var so you can introduce validation logic in the setter, but you couldn't change the type and the storage would still have to be in the super class, so, not sure...

@adriaanm
Copy link
Contributor

jvican added a commit to dotty-jvican/dotty that referenced this issue Jul 1, 2016
When defining a final var, the type is not widened and, thus, it's as
specific as possible. However, when changing the value of the var, it
will give a compilation error because it cannot match the constant
types. This fix widens the type whenever it finds a final var.
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 31, 2016
odersky added a commit that referenced this issue Jul 31, 2016
Fix #1285: Mutable vars are not to be considered constant
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

5 participants