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


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


  • Checking if an ArrayList contains a certain String while being case insensitive
  • Calculating the time difference(in hours and minutes) using strtotime() [closed]
  • Android: openFileOutput throws NullPointerException
  • Laravel Blade - custom method for html block?
  • How can I get the current gps location every time a function is called?
  • Code Map Missing in Visual Studio 15 - Preview 4
  • How to receive GCM message when app is closed or in background?
  • WEBrick fails to start default rails app: “uninitialized constant ActiveRecord::ConnectionAdapters::
  • Creating Neural Network for un-encountered inputs
  • Python mock a base class's attribute
  • Open default mail app from within Qt with some html
  • candlestick plot from pandas dataframe, replace index by dates
  • -(void)viewWillAppear:(BOOL)animated doesn't called
  • Angular4 - Nested route with params
  • Speed up Date#parse & Date#strptime in Ruby, more elegant way or best practice?
  • Symfony 2:image paths in javascript file with assetic
  • Max execution time error
  • Display standard razor/mvc 3 validation messages displayed in another language
  • Working with codeception and laravel
  • How to manipulate content of a comment with Apache POI
  • Laravel 5 - Cache remember doesn't work
  • Jekyll - How do I create pages in the root directory?
  • Execute powershell script on a remote computer using C#
  • Get spring boot pagination number starts from 1 instead of 0
  • Failed to resolve: firebase-auth-15.0.0 [closed]
  • Bundling python(“.py”)files along with java class files for a web application
  • Signed Java web start application with Glassfish 4.1 and Java7
  • Add checkbox dynamically using angular 2
  • Thumbnails for mxml components in Flex
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Annotate objects in a queryset with next and previous object ids
  • Neo4j…how to get a visual representation of my data?
  • Support of :after in IE7
  • Cloud Code: Creating a Parse.File from URL
  • Change cell value based on cell color in google spreadsheet
  • Stacked bar chart with continuous time-axis as x-axis
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • media foundation H264 decoder not working properly