开发者

How to access "overridden" inner class in Scala?

I have two traits, one extending the other, each with an inner class, one extending the other, with the same names:

trait A {
    class X {
        def x() = doSomething()
    }
}

trait B exte开发者_运维技巧nds A {
    class X extends super.X {
        override def x() = doSomethingElse()
    }
}

class C extends B {
    val x = new X() // here B.X is instantiated
    val y = new A.X() // does not compile
    val z = new A.this.X() // does not compile
}

How do I access A.X class in the C class's body? Renaming B.X not to hide A.X is not a preferred way.

To make things a bit complicated, in the situation I have encountered this problem the traits have type parameters (not shown in this example).


trait A {
    class X {
        def x() = "A.X"
    }
}

trait B extends A {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B {
  val self = this:A
  val x = new this.X()
  val y = new self.X()
}

scala> val c = new C
c: C = C@1ef4b

scala> c.x.x 
res0: java.lang.String = B.X

scala> c.y.x
res1: java.lang.String = A.X


For those interested in this exotic issue, I have discovered it works also as a return value of a function. Since my traits A and B have type parameters, it should lead to more concise code:

trait A[T, U, V] {
    class X {
        def x() = "A.X"
    }

    def a = this:A[T, U, V]
}

trait B[T, U, V] extends A[T, U, V] {
    class X extends super.X {
        override def x() = "B.X"
    }
}

class C extends B[SomeClass, SomeOtherClass, ThirdOne] {
    val aVerbose = this:A[SomeClass, SomeOtherClass, ThirdOne] // works but is a bit ugly
    val aConcise = a
    val x = new this.X()
    val y = new aVerbose.X()
    val z = new aConcise.X()
}

scala> val c = new C()
c: C = C@1e852be

scala> c.x.x()
res2: java.lang.String = B.X

scala> c.y.x()
res3: java.lang.String = A.X

scala> c.z.x()
res4: java.lang.String = A.X
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜