How to access “overridden” inner class in Scala?

随声附和 提交于 2019-12-12 13:28:26

问题


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 extends 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).


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/2436555/how-to-access-overridden-inner-class-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!