Access modifier for extensions

Vladimir Panteleev vladimir at thecybershadow.net
Thu Jan 16 10:50:25 PST 2014


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.


More information about the Digitalmars-d mailing list