- scala> class X (val i:Int)
- defined class X
- scala> class Y(i:Int) extends X(19) {
- | println(i)
- | }
- defined class Y
- scala> val y = new Y(100)
- 100
- y: Y = Y@3e55a58f
- scala> y.i
- res7: Int = 19
This begs the question: how can the subclass access the superclass instance of i? Let me re-iterate. Just because it is possible this is a bad idea. This pattern seems like it is begging to create bugs. But who knows there is probably some reason out there where this is required.
The way to access the X.i value from Y is to declare which value you want using the syntax this:X :
- scala> class Y(i:Int) extends X(19) {
- | println("Y.i"+i)
- | println("X.i"+(this:X).i)
- | }
- defined class Y
- scala> new Y(39)
- Y.i39
- X.i19
- res8: Y = Y@451dfada
Repeat: Watch out! Because I learned this danger while writing this topic:
- scala> class Y(i:Int) extends X(19) {
- | println("1. "+i)
- | println("2. "+this.i)
- | println("3. "+(this:Y).i)
- | println("4. "+(this:X).i)
- | }
- defined class Y
- scala> new Y(44)
- 1. 44
- 2. 44
- 3. 19 // this is created via (this:Y).i !
- 4. 19
- res0: Y = Y@338bd37a
It seems that the syntax (this:X).i accesses the public accessor for i. The public access is a method call I think so it always obtains the value for the property. Shocking!
I'm using a nightly build of Scala 2.8 (2.8.0.r19410-b20091106023416). The class Y won't compile without specifying override. I wonder if it's now illegal in Scala 2.8 to shadow members?
ReplyDeleteI just downloaded the nightly build 2.8.0.r20174-b20091217020115 and it compiles fine. Are you sure you didn't specify Y as follows:
ReplyDeleteclass Y(val i:Int) extends X(19)
or
case class Y(i:Int) extends X(19)
because either of these will require an override because the i in Y is public.