In Scala, what happens when an internal object extends an enclosing class?

I wrote a matrix class in Scala, and I thought that the best way to implement the transpose operation is to simply return the interface with all the "transposed" operations. Therefore, if it matrix.apply(i, j)returns the (i, j) th element, then matrix.transpose returns an interface (but not a copy of the data) that contains the apply method, which returns the matrix (j, i). And I wrote this interface like this (what I really wrote is much more random):

abstract class Matrix {
   def apply(i : Int, j : Int) : Double
   //other matrixy operations
   private def outer = this //so the inner can see the enclosing instance
   object TransposedInterface extends Matrix {
      def apply(i :Int, j : Int) = outer(j, i)
   }
}

Which one is cute, I think, but now TransposedInterfacealso has an object inside it, called TransposedInterface, and so on, recursively, and where does it all end?

I tried the following in the interpreter:

class Outer(val i : Int) {
   object Inner extends Outer(i + 1)
}

val o = new Outer(1)
o.Inner.Inner.Inner.Inner.i

5, , .

? ? , , . ? - , ?

+5
1

, , .

object Foo { ... } lazy val Foo = { ... }. , , , , , ( - , ) , .class. (, , , , , , .)

+3

All Articles