Adding a new design constraint to D

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Wed Jun 15 07:28:11 UTC 2022


On Wednesday, 15 June 2022 at 06:55:35 UTC, Dom Disc wrote:
> On Tuesday, 14 June 2022 at 22:08:31 UTC, Zoadian wrote:
>>> Ok, you got me.
>>> Didn't thought about friends of sub-classes.
>>> But in D I don't know ... maybe something similar is possible?
>>
>> should it even be possible? does anyone write such code in 
>> practice? it looks confusing as hell to me.
>
> It would become even more ugly if there are multiple private 
> variables, because in that case you would need to put each of 
> them in its own sub-class with its own set of friends.
>
> But the point is: in C++ it IS possible to encapsulate each 
> variable so, that it can be accessed only by an explicitly 
> defined set of functions (the friends of the subclass). This is 
> today not possible in D.

This isn't something you want to do frequently, but it makes 
sense in the rare case where you deal with deadlock issues or 
other dangerous things! :-)

While module-based encapsulation is ok for modest complexity 
situations, it is sometimes useful to make everything 
object-private and then open up when you need it.

I recently ported some big javascript thing to TypeScript and it 
was very useful to first convert to classes, then make all fields 
private first, then remove the privacy until the compilation was 
successful. That gave me a better understanding of what the 
structure was.



More information about the Digitalmars-d mailing list