Adding a new design constraint to D

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun Jun 19 08:10:28 UTC 2022


On Sunday, 19 June 2022 at 07:41:37 UTC, Ola Fosheim Grøstad 
wrote:
> On Sunday, 19 June 2022 at 07:07:07 UTC, Max Samukha wrote:
>> The special shadowing rules do not seem to introduce any 
>> logical inconsistencies.
>
> It is inconsistent with scoping semantics: classes within 
> classes, structs within structs, and you are allowed to shadow 
> globals. Yet shadowing when subclassing is allowed?
>
> It is just a personal style guideline that is forced on 
> everybody for no particular reason, it is not a common mistake 
> to make if you know what you are doing.

This is also legal, if you care about shadowing then this is a 
much more likely problem:

```D

class A {
     int x;
     void increment(){
         // lots of code
         int x;
         // lots of code
         x++;
     }
}
```

So that scoping restriction is completely arbitrary.

I believe Rust allows rebinding the name within the same scope, 
which might be unreasonable, but there is one advantage: you get 
to use a consistent naming scheme which makes code easier to read.



More information about the Digitalmars-d mailing list