Using closure in function scope to make "real" private class members

norm norm.rowtree at gmail.com
Mon Jun 6 04:01:27 UTC 2022


On Monday, 6 June 2022 at 01:05:44 UTC, forkit wrote:
> On Sunday, 5 June 2022 at 19:41:36 UTC, Dom Disc wrote:
>> On Saturday, 4 June 2022 at 14:40:10 UTC, zjh wrote:
>>> For me, it would be better ,if I could encapsulate at `class 
>>> level`.In any case, I don't want the `outside` to affect `my 
>>> class inside`.
>>
>> C++ "class level privacy" needs to have "friends", else you 
>> cannot create top-level functions that have access to the 
>> private members.
>> But these "friends" destroy the whole purpose of "private":
>> Anybody can declare friends, without even touching the file 
>> where the class is defined, and thereby access things that 
>> shouldn't even be visible to them (implementation details or 
>> even security relevant things)!
>> In D you need to change the file where a class is defined if 
>> you need to access its privates - this is much easier visible 
>> and in a larger team can't be hidden from the person 
>> responsible for that class.
>
> What I like about C++ friend concept, is that you explicately 
> know who is a friend and who isn't. It's declared right there 
> in the class.
>
> In a D module, everything could be a friend. Everything! You'll 
> have to look at ALL the code in the module to determine that - 
> as opposed to C++, where you can find this information right 
> there in the class declaration.
>
> Imagine if everyone in your street was your friend, and they 
> could come around willy nilly and do as they please, in your 
> nice, encapsulated home.
>
> Now, to be fair, it's nice to have a friend around. But all the 
> time!!! Doing whatever the %F#V they want!!!

This is such a non-issue, the mindset in D simply needs to be 
encapsulation boundary is at the module level. All this huff and 
puff of wasted of energy goes away at that point and your designs 
will actually be much cleaner than in C++. This is a pure 
ideological debate and it is actually very useful to have module 
level encapsulation.

It is even more of a non-issue when D has thread-local variables 
by default.


More information about the Digitalmars-d mailing list