www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Protected visiblity expansion

reply Richard (Rikki) Andrew Cattermole <richard cattermole.co.nz> writes:
Here is an idea to satisfy a lot of people: upgrading the 
protected visibility modifier to include a symbol.

The default is to use the this pointer or the current module.

```d
module a;

protected void freeFunc() {
}

class Class {
     protected void method() {
     }
}
```

```d
module b;
import a;

void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // error
}
```

If we provide it a symbol or module/package it'll limit to all 
symbols below that.

```d
module pack.a;

protected(pack.b.check) void freeFunc() {
}

class Class {
     protected(pack.b.check) void method() {
     }
}
```

```d
module b;
import a;

void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // error
         }
     }

     Child child = new Child;
     child.method; // error
}

void check() {
     freeFunc; // ok

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // ok
}
```

To spice it up one more level, let's also make it accept multiple 
symbols.

That should cover pretty much every hole in visibility modifiers 
that people could dream up!
Dec 01
next sibling parent claptrap <clap trap.com> writes:
On Monday, 1 December 2025 at 22:30:56 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 Here is an idea to satisfy a lot of people: upgrading the 
 protected visibility modifier to include a symbol.

 The default is to use the this pointer or the current module.

 ```d
 module a;

 protected void freeFunc() {
 }

 class Class {
     protected void method() {
     }
 }
 ```

 ```d
 module b;
 import a;

 void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // error
 }
 ```

 If we provide it a symbol or module/package it'll limit to all 
 symbols below that.

 ```d
 module pack.a;

 protected(pack.b.check) void freeFunc() {
 }

 class Class {
     protected(pack.b.check) void method() {
     }
 }
 ```

 ```d
 module b;
 import a;

 void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // error
         }
     }

     Child child = new Child;
     child.method; // error
 }

 void check() {
     freeFunc; // ok

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // ok
 }
 ```

 To spice it up one more level, let's also make it accept 
 multiple symbols.

 That should cover pretty much every hole in visibility 
 modifiers that people could dream up!
You stole me idea!
Dec 02
prev sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Monday, 1 December 2025 at 22:30:56 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 Here is an idea to satisfy a lot of people: upgrading the 
 protected visibility modifier to include a symbol.

 The default is to use the this pointer or the current module.

 ```d
 module a;

 protected void freeFunc() {
 }

 class Class {
     protected void method() {
     }
 }
 ```

 ```d
 module b;
 import a;

 void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // error
 }
 ```
Sorry if this is obvious to you, but I can't recall ever writing code with protected in it. Shouldn't child.method not be an error under the current rules since you are accessing it through the derived class?
Dec 02
parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 03/12/2025 12:27 AM, jmh530 wrote:
 On Monday, 1 December 2025 at 22:30:56 UTC, Richard (Rikki) Andrew 
 Cattermole wrote:
 Here is an idea to satisfy a lot of people: upgrading the protected 
 visibility modifier to include a symbol.

 The default is to use the this pointer or the current module.

 ```d
 module a;

 protected void freeFunc() {
 }

 class Class {
     protected void method() {
     }
 }
 ```

 ```d
 module b;
 import a;

 void main() {
     freeFunc; // error

     class Child : Class {
         this() {
             this.method; // ok
         }
     }

     Child child = new Child;
     child.method; // error
 }
 ```
Sorry if this is obvious to you, but I can't recall ever writing code with protected in it. Shouldn't child.method not be an error under the current rules since you are accessing it through the derived class?
https://dlang.org/spec/attribute.html#protected You can only access inside the derived class, or in its original module that defined the method.
Dec 02
parent jmh530 <john.michael.hall gmail.com> writes:
On Tuesday, 2 December 2025 at 21:20:52 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 On 03/12/2025 12:27 AM, jmh530 wrote:
 On Monday, 1 December 2025 at 22:30:56 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 [...]
Sorry if this is obvious to you, but I can't recall ever writing code with protected in it. Shouldn't child.method not be an error under the current rules since you are accessing it through the derived class?
https://dlang.org/spec/attribute.html#protected You can only access inside the derived class, or in its original module that defined the method.
I read that text like 5 times before I posted and it still is a little tricky.
Dec 02