-
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
The meaning of this.field
in class body
#15723
Comments
Yes, there are two copies of @SourceFile("test.scala") class B extends Object {
def <init>(y: Int): Unit =
{
this.y = y
super()
println(scala.Int.box(y))
this.foo()
()
}
private val y: Int
def y(): Int = this.y
def foo(): Unit = println(scala.Int.box(this.y()))
}
@SourceFile("test.scala") class C extends B {
def <init>(y: Int): Unit =
{
this.y = y
super(10)
()
}
private val y: Int
override def y(): Int = this.y
} This might indeed be surprising. On the other hand, Scala 2 does the same thing, so any change here would be an incompatible runtime change. Is it worth the cost of doing this? |
My conjecture is that no real-world code depends on such subtle semantics. Otherwise, people would have complained about it for Scala 2. If a real-world program does depend on such subtle semantics, then it should be a bug in the program. I can understand that some magic must happen for class parameters used in super constructor calls. However, if we can make the semantics more consistent in the class body, at least for qualified select like |
Could we disallow overriding a class parameter unless the overridden val is passed to the supercall (as in |
Don't laugh, but I was surprised we can reference a simple class param with The spec calls the getter member introduced by val an alias of the parameter. To my limited thinking, that class param is not a class member, but simply a constructor parameter, by analogy to ordinary use of method param There was an ancient ticket wondering why |
I think such kind of restriction will be helpful. Is it possible to go one step further to disallow overridding class parameters --- if the values are required to be the same, then there is no need to override, just pass it to super class. |
Can someone try this and evaluate what the breakage would be? |
There is a longstanding feature request for "pass-thru" parameters, to express |
That can be accomplised by writing |
|
I think as a first step, it's probably OK to treat |
Now the usage of This check can be implemented in the initialization checker. WDYT? /cc: @olhotak |
Yes, I think we could add a warning for the unqualified |
Compiler version
Scala 2 and Scala 3 (master)
Minimized code
Output
Expectation
I would expect
this.y
to have the same semantics both in the class body and method body.It's up to discussion whether an unqualified
y
in class body and in method body should have the same semantics.The text was updated successfully, but these errors were encountered: