UFCS & overloaded property getters/setters

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Fri Jun 13 13:16:45 PDT 2014


On Friday, 13 June 2014 at 18:18:21 UTC, H. S. Teoh via
Digitalmars-d wrote:
> Aliasing super.prop to .prop in the derived class didn't work, 
> in fact,
> it made things worse; now I have an infinite loop because all
> occurrences of .prop get redirected back to the base class 
> (including
> the setter), and there is no way to alias only the 
> non-overridden getter
> overload, so a call to Derived.prop will end up in the base 
> class method
> where it shouldn't.

Works for me. Following code prints
      base getter
      base getter
      derived setter
Am I missing something?

      import std.stdio;

      class Base {
          int propImpl;
          final @property int prop() {
              writeln("base getter");
              return propImpl;
          }
          @property void prop(int newVal) {
              writeln("base setter");
              propImpl = newVal;
          }
      }

      class Derived : Base {
          alias prop = super.prop;

          override @property void prop(int newVal) {
              writeln("derived setter");
              //super.prop(newVal);
              //doSomethingElse(newVal);
          }

          void someOtherMethod() {
              auto x = prop;
              auto y = super.prop;
              prop = x;
          }
      }

      void main() {
          auto d = new Derived;
          d.someOtherMethod();
      }


More information about the Digitalmars-d mailing list