views:

213

answers:

2

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

+9  A: 
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
Rex Kerr
Thanks a lot, I don't remember encountering this kinda syntax in Odersky's Programming in Scala book. I admit this case is quite an edgy :)
doom2.wad
Nice, really! +1
incarnate
A: 

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
doom2.wad