Reading chapter 20 of Ordesky's book on Scala, I found that inner classes are path dependant. Among other features, that implies that they can only be instantiated within the outer class or giving an outer class instance.
The question arises: I would like to implement an static inner class in Scala but the author suggest that is not possible.
I immediatelly thought of making the "inner class" (lets call it Inner) a member of Outer's companion object.
The accepted answer of this question seems to point towards that direction.
But that drives to a problem: Inner's type ins't Outer#Inner, I could try something like:
object Outer {
class Inner extends Outer#Inner { }
}
This doesn't work however. Do you know a work arround for this? I have the hunch that it could be done with abstract types but I am not sure.
Note that making Inner an inner class of the companion objects is not exactly as having a non-path-dependant Inner class because of its type.
Yes, that's the closest Scala equivalent.
This isn't a problem, because
Outer#Inneris equivalent to a non-static inner class ofOuterin Java. I.e. it has a reference to anOuterobject.If you want to create a non-companion inner class which can't be used path-dependently, it isn't possible. You are free to always write
Outer#Innerinstead ofo.Innerin your code, of course.