On Thursday, 16 January 2014 at 18:50:26 UTC, Vladimir Panteleev wrote:
On Thursday, 16 January 2014 at 09:28:18 UTC, Boyd wrote:
For extending, there is the 'protected' attribute, but it's specific for class overriding only. Very often, extensions are not merely limited to derived classes. What I find myself wanting is more like the 'package' attribute, except it needs to work outside of the package as well.

You can make class members accessible to a module by reintroducing them in a derived class using an alias. It even works for static and final methods, and field variables. Example:

///////////////////////////////////////////////////////////

module library;

class Button
{
        protected void click() {}
}

///////////////////////////////////////////////////////////

module user;

import library;

void main()
{
        auto button = new Button();
        button.click(); // NG
}

///////////////////////////////////////////////////////////

module extender;

import library;

class ExtendedButton : Button
{
        // reintroduce protected member as private to this scope
        private alias Button.click click;
}

void main()
{
        auto button = new ExtendedButton();
        button.click(); // OK
}

///////////////////////////////////////////////////////////

HTH.

While it would work in this case, extending the class is not always an option. Plus it seems quite a big and dirty-looking work around for something that should, in my opinion, be quite simple.

Reply via email to