Scope modules DIP rough messy draft.

12345swordy alexanderheistermann at gmail.com
Mon Oct 29 14:45:19 UTC 2018


On Monday, 29 October 2018 at 01:17:06 UTC, luckoverthere wrote:
> How is encapsulation going to be beneficial in a situation 
> where the class can only be used from inside that function?
That because, nested classes are NOT encapsulated in the function!
The module itself is the encapsulation unit, not the class. (I 
learn that the hard way)

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

This compiles btw.

> 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.

> 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.

> 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.

> Lol you had to google that and it's a link to a stackoverflow 
> question no less.
You are creating a false equivalence by comparing D modules with 
C++ namespace.
You can import modules in a class in D, you can't import a 
namespace in class in C++. Ergo your comparison fall flat.

> Their implementation however, does not allow modules to be 
> defined in a function or class.

Irrelevant.

> And you already have that encapsulation because it is nested.

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


> Seems your misunderstanding here, nested modules, as in nested 
> in a function/class.

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

> Your right, it'll be much harder to convince andrei and walter 
> than it would be to convince me.
It seems that way if you oppose it on ideological grounds.
Regardless, apparently I need to dedicate a section that is 
called "Only the module itself is the unit of encapsulation" in 
the DIP.



More information about the Digitalmars-d mailing list