-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
So there is such a thing as a |
What does |
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. |
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} |
@adriaanm: any idea if the semantics of |
As @DarkDimius said, currently, |
The logic around widening inferred types is here: https://github.com/scala/scala/blob/2.12.x/src/compiler/scala/tools/nsc/typechecker/Namers.scala#L839 |
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.
Fix #1285: Mutable vars are not to be considered constant
Uh oh!
There was an error while loading. Please reload this page.
The text was updated successfully, but these errors were encountered: