Why do private member variables behaved like protected in the same module when creating deriving class?

Atila Neves atila.neves at gmail.com
Thu Nov 1 14:17:13 UTC 2018


On Monday, 29 October 2018 at 23:15:58 UTC, unprotected-entity 
wrote:
> On Monday, 29 October 2018 at 15:50:56 UTC, Jonathan M Davis 
> wrote:
>>
> It's not (just) that I don't like it, it's that I don't like 
> not giving the programmer the tool to better encapsulate their 
> code (specifically classes) *within* a module.

You don't need to protect code in your module from other code in 
your module. If you do, put them in different modules.

> In D however, it's exactly how you're meant to do it - it's by 
> design - it's reaching out and saying, this is how you should 
> do it.

It _is_ how you should do it, but not with giant modules. Just 
have smaller modules.

>  D seems to be proud, that you cannot encapsulate a class, 
> within a module, from other code also in that module. If 
> C++/C#/Java took that view, imagine the outcry.

Java doesn't compare - every class is in its own file. If you 
want to write D code Java style, and have `private` work the same 
way, then have a class per module.

As for C++, one could argue that it encourages the practice of 
having source files implement
multiple classes, which is part of the reason anyone uses 
`friend` there anyway!

> And given what I've seen in phobos..well... I'm still looking 
> for the 'design'.

There are parts of Phobos that aren't very well designed, 
especially the older code. There are also parts that are great 
(std.experimental.allocator for instance).

> All I see are gigantic modules full of code...that does who 
> knows what to each other..cause they're all friends and can do 
> whatever they like to each other, in D.

Yeah, that's not good. But the barrier to change the standard 
library is higher so I'm mostly ok with that.




More information about the Digitalmars-d mailing list