Adding a new design constraint to D

Dom Disc dominikus at scherkl.de
Wed Jun 15 06:55:35 UTC 2022


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.

We can make it possible (and much less ugly) with two UDAs, one 
to hide the variables and one to explicitly allow access to 
specific variables.
Of course we shouldn't name them like in my previous post.
Better names would be @hidden and @sees(x, y, z). I think 
everybody able to speak english can easily guess what these 
should mean, even without reading the spec.

Unlike @hidden alone this allows us to do things that are 
currently impossible, so it brings real benefit.


More information about the Digitalmars-d mailing list