Scope modules DIP rough messy draft.

Stanislav Blinov stanislav.blinov at gmail.com
Mon Oct 29 15:00:07 UTC 2018


On Monday, 29 October 2018 at 14:45:19 UTC, 12345swordy wrote:
> On Monday, 29 October 2018 at 01:17:06 UTC, luckoverthere wrote:

> int func()
> {
>     class Inner
>     {
>         private int x;
>     }
>     Inner ex = new Inner();
>     ex.x = 1;
>    globalvar = ex.x;
>    return ex.x;
> }
>
> This compiles btw.

So? You, the programmer, are breaking encapsulation here by 
accessing 'private' member. You wrote that code, you better know 
what 'x' is. Your argument seems to be based on an assumption 
that you don't have to know. But you do, otherwise you shouldn't 
be writing code.

>> That wasn't the question.
> You ask "what are the benefits", I had give you the answer: The 
> Dip will address the limitations of nested functions and 
> provide a solution for them via nested modules.

That'd be interesting to see. Why didn't you put those supposed 
limitations in there to begin with? I.e. what's the purpose of 
this "messy draft"?

>> In this case the function/class is the unit of encapsulation
> No, they are NOT! The module IS unit of encapsulation. Period. 
> Global variables can accessing anything provided that they are 
> in the same module.

Global variables aren't accessing anything. You (the programmer) 
are, by assigning values to them.

>> That's the point of them, they aren't accessible to anyone, 
>> that's the encapsulation.
> If they are in the same module, yes you can! If you try to 
> create a global pointer outside the function that points to 
> local private variable inside the function in the same module 
> you are technically accessing it! You just got a dangling 
> pointer after the function is called.

That's not an issue with encapsulation at all. That's a lifetime 
issue.

```
int* global;

void main() @safe {
     int x;
     global = &x;
}

$ dmd -dip1000 test.d
test.d(5): Error: address of variable x assigned to global with 
longer lifetime
```

> Nested classes/functions are not encapsulated. The module 
> itself is the unit of encapsulation.

You're confusing encapsulation and scopes now:

void main() {
     void bar() {}
}

void foo() {
     main.bar(); // Error: no property bar for type void
}




More information about the Digitalmars-d mailing list