- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: generated outer refs in subclasses
Thu, 2012-02-02, 20:24
Thanks! I tried following:
class SomeClass {
class A1
}
class SomeOtherClass extends SomeClass {
class A2
class B1 extends super.A1
// class B2 extends A2, or equivalently:
class B2 extends this.A2
}
def main(args : Array[String]) : Unit = {
val soc = new SomeOtherClass
val b1 = new soc.B1
val b2 = new soc.B2
println
}
The b1 has 2 (identical) outer references, the b2 has only 1. In the
equivalent code in Java both b1 and b2 have 2 (identical) outer
references, so scala improves here over java already in the "extends
this._" case. So it might be possible to improve also in the "extends
super._" case. As long as this and super are by definition the same
instance, which I thought is the case.
(I know it is more of an aesthetic point, but if I have inheritance
depth 4, then I'd have 4 identical outer instances in my nested Log
classes, which is sort of ugly...)
Best regards,
Jan
On 02.02.2012 17:42, Paul Phillips wrote:
> [I'm not on scala-user.]
>
> On Thu, Feb 2, 2012 at 7:57 AM, Jan Vanek wrote:
>> Now the l2 contains only one "outer" reference o. The code is however more
>> verbose.
>>
>> Will it be optimized sometime in the future?
> Unlikely. They happen to point to the same thing in your example, but
> they need not.
>
> class SomeClass {
> def f1 = 5
>
> class A1 {
> class Log { def x = f1 }
> }
> }
>
> class SomeOtherClass(val someClass: SomeClass) {
> def f2 = 10
>
> class B1 extends someClass.A1 {
> class Log extends super.Log { def y = x + f2 }
> }
> }
>
On 03.02.2012 01:05, Paul Phillips wrote:
> You altered my example to remove the part which was instructive. You
> have SomeOtherClass extending SomeClass, but there is nothing which
> requires this. Your superclass could be nested in some completely
> different instance of a completely different class, and the outer
> pointer is necessary to call methods within that completely different
> instance.
Sorry, I didn't ignore it. In your original example the B1 class has and
needs 2 different outer pointers. The nested Log class (extending
super.Log) has again, regardless of who is a super-class of B1, 2
identical outer pointers. It is the Log class which I am interested in.
So I wanted to present a minimal example where scala generates only 1
outer pointer in a nested sub-class.
> Furthermore, the outer field has to be generated for the
> base class, which has no knowledge of its subclasses.
Sure, but the sub-class always knows its base class, so the sub-class
knows (or can know) that the outer pointer in the base-class (which as
you say has to be generated) is already the outer pointer (same
instance) to its own enclosing outer class. Scala recognizes it for the
"extends this._" case and maybe could for the "extends super._" case.
> Also, super and this being the same instance does not mean that
> selections on super and this are the same.
> class A {
> trait Inner { def f = 1 }
> }
> class B extends A {
> trait Inner extends super.Inner { override def f = 2 }
> object o1 extends this.Inner
> object o2 extends super.Inner
> }
>
> object Test {
> def main(args: Array[String]): Unit = {
> val x = new B
> println(x.o1.f)
> println(x.o2.f)
> }
> }
Thanks. If I turn the Log class into trait there is indeed only 1 outer
pointer.
With regards,
Jan