www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - protected + package attributes

reply zodd <zodd maill.com> writes:
Suppose I have a class with a few protected functions. I want to 
let another class from the same package call these functions. 
Thus I've added a "package" attribute and got the following:
Error: conflicting protection attribute 'package' and 'protected'

How can I achieve what I want? These member functions must be 
protected, I can't make them private because this is a base class 
intended for inheritance.
Jul 11 2016
parent reply ag0aep6g <anonymous example.com> writes:
On 07/11/2016 02:28 PM, zodd wrote:
 Suppose I have a class with a few protected functions. I want to let
 another class from the same package call these functions. Thus I've
 added a "package" attribute and got the following:
 Error: conflicting protection attribute 'package' and 'protected'

 How can I achieve what I want? These member functions must be protected,
 I can't make them private because this is a base class intended for
 inheritance.
Can have only one level of protection. If package is too restrictive with regards to overriding, and protected is too restrictive with regards to calling, then there's only public left. Other than that, you could add a `package` method with a different name that just calls the `protected` one.
Jul 11 2016
next sibling parent Mike Parker <aldacron gmail.com> writes:
On Monday, 11 July 2016 at 12:42:57 UTC, ag0aep6g wrote:

 Other than that, you could add a `package` method with a 
 different name that just calls the `protected` one.
This is a pattern I have found useful, particularly when dealing with protected abstract methods, e.g. package void someActon() { doSomeAction(); } protected abstract void doSomeAction;
Jul 11 2016
prev sibling parent zodd <zodd maill.com> writes:
On Monday, 11 July 2016 at 12:42:57 UTC, ag0aep6g wrote:
 On 07/11/2016 02:28 PM, zodd wrote:
 Suppose I have a class with a few protected functions. I want 
 to let
 another class from the same package call these functions. Thus 
 I've
 added a "package" attribute and got the following:
 Error: conflicting protection attribute 'package' and 
 'protected'

 How can I achieve what I want? These member functions must be 
 protected,
 I can't make them private because this is a base class 
 intended for
 inheritance.
Can have only one level of protection. If package is too restrictive with regards to overriding, and protected is too restrictive with regards to calling, then there's only public left. Other than that, you could add a `package` method with a different name that just calls the `protected` one.
It seems that I have no other choice. Thanks for a suggestion!
Jul 11 2016