0

I have a class A and it extends B and mixes in C like the code below. How do I make it work?

class A extends B with C {
  @override
  int getValue() {
    // Call's C's getValue.
    super.getValue();
    // Now I want to call B's getValue. How do I do that?
    return B.getValue(); // Doesn't work.
  }
}

class B {
  int getValue() {
    return 1;
  }
}

class C {
  int getValue() {
    return 2;
  }
}

How can I execute B's getValue? Thanks.

lrn
  • 64,680
  • 7
  • 105
  • 121
empt
  • 133
  • 1
  • 10
  • does [this](https://stackoverflow.com/questions/57035710/how-to-call-super-class-method-if-there-is-a-mixin-with-a-method-with-the-same-n) help you? – Shubham Gupta Aug 18 '20 at 04:00
  • The linked answer only tells you how to reorder mixins to make one of them callable using super. It does not provide a way to call multliple of them, or to call the member from the superclass, so I don't think it answers this question. – lrn Aug 19 '20 at 06:45

1 Answers1

1

The mixin method from C shadows the method from B, so you can't use super directly to refer to the B method. You need to introduce a way to access it.

What you can do is to put a private redirecting function in between the B class and the C mixin application:

class A extends B with _Helper, C {
  int getValue() {
    // It's C
    super.getValue();
    // Now I want to get B's getValue how do it?
    return super._getValueFromB();
  }
}
    
class B {
  int getValue() {
    return 1;
  }
}
mixin C {
  int getValue() {
    return 2;
  }
}
mixin _Helper on B {
  int _getValueFromB() => super.getValue();
}

Since the mixin application order is B, B-with-_Helper, B-with-_Helper-with-C, the super of the B-with-_Helper superclass is B, and the _getValueFromB will access it correctly.

lrn
  • 64,680
  • 7
  • 105
  • 121