Invoking super in case of identical name

I cannot find a way to fix the code below in a way that makes it compile. I tried using import A.C as D and type D = A.C to no avail.

object A:
  class C: // May not be renamed
    def foo: Unit = () // May not be renamed

object B:
  class C extends A.C: // May not be renamed
    override def foo: Unit = super[A.C].foo // May not be renamed

Probably i am completely missing the point of your question, but would not super.foo just work? Thus by removing [A.C].

1 Like

Sorry, I oversimplified the code! Below is an updated version.

object A:
  class C: // May not be renamed
    def foo: Int = 42 // May not be renamed

trait E:
    def foo: Int = -1 // May not be renamed

object B:
  class C extends A.C, E: // May not be renamed
    override def foo: Int = super[A.C].foo // May not be renamed

This does not change much, use super[E].foo for overriding foo on E and super[C].foo for overriding foo on A.C. The former is also the default.

1 Like

Thanks! Duh. I somehow did not consider the possibility that an unqualified C would refer to A.C in that context.

It does make me wonder about the case below (which luckily I have not encountered)…

object A:
  class C: // May not be renamed
    def foo: Int = 42 // May not be renamed

object E:
  trait C: // May not be renamed
    def foo: Int = -1 // May not be renamed

object B:
  class C extends A.C, E.C: // May not be renamed
    override def foo: Int = super[C].foo // May not be renamed

Hmm, i see your point, actually i expected something like this to work:

object A:
  class C: // May not be renamed
    def foo: Int = 42 // May not be renamed

object E:
  trait C: // May not be renamed
    def foo: Int = -1 // May not be renamed

object B:
  import A.C as X
  import E.C as Y
  class C extends X, Y: // May not be renamed
    override def foo: Int = super[Y].foo // May not be renamed
    
val c = new B.C
println(c.foo)

but i doesn’t:

Y does not name a parent of class C

This statement seems incorrect?!

I see the ticket was boosted with a sad face :frowning_face: during pandemic.

Or maybe it’s a more nuanced emoji. Distraught?

3 Likes