-
Notifications
You must be signed in to change notification settings - Fork 21
Self Type shadows constructor arguments. #3861
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
Imported From: https://issues.scala-lang.org/browse/SI-3861?orig=1 |
@paulp said: And your ticket made me realize that this bug represents a workaround to that one! scala> class A(var x: Int)
defined class A
scala> class B(x: Int) extends A(x) { def f(z: Int) = this.x = z }
<console>:6: error: reassignment to val
class B(x: Int) extends A(x) { def f(z: Int) = this.x = z }
^
scala> class B(x: Int) extends A(x) { self: A => def f(z: Int) = this.x = z }
defined class B
scala> new B(10)
res0: B = B@32e6e42e
scala> res0.x
res1: Int = 10
scala> res0.f(-75)
scala> res0.x
res3: Int = -75 Applying one bug to work around another: that's how we know we're engineers. |
@dcsobral said: scala> trait Y
defined trait Y
scala> abstract class X(x: Int) { self: Y => x }
defined class X
scala> scala> object X {
| val x: Int = 1
| trait Y
| abstract class XX(x: Int){ self: Y =>
| println(x)
| }
| new XX(2) with Y
| }
defined module X
scala> X
1
res0: X.type = X$$@1df3082
Replaying 2 commands from interpreter transcript.
defined module X
2
res3: X.type = X$$@1e1370ab
|
It seems that the self-type annotation shadows constructor arguments.
Of course, without self-type annotation, there is no problem,
and curiously there is no problem with 'val',
This problem, for example, will cause the following unexpected result,
The text was updated successfully, but these errors were encountered: