Consider this code:
class Outer {
class Inner
}
In Java it would be possible to create an instance of Inner with:
Outer.Inner inner = new Outer().new Inner();
I know I can write this in Scala:
val outer = new Outer
val inner = new outer.Inner
But I wonder if it is possible to express the same without the assignment to outer.
Both
new Outer.new Inner
and
new (new Outer).Inner
are not accepted by the compiler.
Is there something I’m missing?
First of all, I doubt that the instantiation in one go is any meaningful — you are like throwing away the
Outerinstance, keeping no reference to it. Makes me wonder, if you weren’t thinking of a Java static inner class, likewhich in Scala would translate to
Innerbeing an inner class ofOuter‘s companion object:If you really want an inner dependent class, and you just want more convenient syntax for instantiating it, you could add a companion object for it: