-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
abstract var
can only be overridden when the override
modifier is not used
#21149
Comments
Hey, so this is actually the intended behaviour, we do not allow to override vars. What we maybe could consider is adjusting the spec, since we actually don't mention it there, I think. (cc @sjrd) Either way, closing this. |
I see. In this case should |
overriding is not the same thing as implementing, overriding changes signature, implementing is fine because the types stay the same |
I find this confusing. For |
you actually can not override a function to have a more precise argument, meaning you can not override a setter, so it makes no sense to override a var, which would override the setter: scala> trait Foo:
| def setFoo(newFoo: Any): Unit
| trait Bar extends Foo:
| override def setFoo(newFoo: String): Unit
|
-- [E038] Declaration Error: ---------------------------------------------------
4 | override def setFoo(newFoo: String): Unit
| ^
| method setFoo has a different signature than the overridden declaration
|
| longer explanation available when compiling with `-explain`
1 error found |
I don't think that applies to my examples, in trait C:
var x: Int
class D extends C:
override var x: Int = 10 The type for |
ok, comparing to scala 2 behavior it would seem attempt the override as long as the type does not change: scala> trait Foo {
| var x: Any}
trait Foo
scala> class Bar extends Foo {
| override var x: Any = 23
| }
class Bar
scala> class Bar extends Foo {
| override var x = 23
| }
^
error: class Bar needs to be abstract.
Missing implementation for member of trait Foo:
def x_=(x$1: Any): Unit = ??? // an abstract var requires a setter in addition to the getter
override var x = 23
^
On line 2: error: variable x overrides nothing.
Note: the super classes of class Bar contain the following, non final members named x_$eq:
def x_=(x$1: Any): Unit |
saying this is duplicicate of #18692 because the error message is the same |
Compiler version
3.4.2
Minimized code
File1:
File2:
Output
File1
compiles,File2
fails withExpectation
Both
File1
andFile2
should compile.The text was updated successfully, but these errors were encountered: