- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
generated outer refs in subclasses
Thu, 2012-02-02, 16:57
Hallo,
when I have:
class A1 { class Log { var x = 0 } def createLog = new Log } class B1 extends A1 { class Log extends super.Log { var y = 0 } override def createLog = new Log }
val b1 = new B1 val l1 = b1.createLog scala generates two outer references in the B1#Log class. When inspecting the instance l1 both outer references point to the same object.
It is possible to do:
object A2 { class Log(val o: A2) { var x = 0 } } class A2 { def createLog = new A2.Log(this) } object B2 { class Log(override val o: B2) extends A2.Log(o) { var y = 0 } } class B2 extends A2 { override def createLog = new B2.Log(this) }
val b2 = new B2 val l2 = b2.createLog
Now the l2 contains only one "outer" reference o. The code is however more verbose.
Will it be optimized sometime in the future?
Thanks and regards,Jan
when I have:
class A1 { class Log { var x = 0 } def createLog = new Log } class B1 extends A1 { class Log extends super.Log { var y = 0 } override def createLog = new Log }
val b1 = new B1 val l1 = b1.createLog scala generates two outer references in the B1#Log class. When inspecting the instance l1 both outer references point to the same object.
It is possible to do:
object A2 { class Log(val o: A2) { var x = 0 } } class A2 { def createLog = new A2.Log(this) } object B2 { class Log(override val o: B2) extends A2.Log(o) { var y = 0 } } class B2 extends A2 { override def createLog = new B2.Log(this) }
val b2 = new B2 val l2 = b2.createLog
Now the l2 contains only one "outer" reference o. The code is however more verbose.
Will it be optimized sometime in the future?
Thanks and regards,Jan