how to make private class member private

bauss jj_1337 at live.dk
Sat Mar 17 14:16:19 UTC 2018


On Saturday, 17 March 2018 at 11:08:27 UTC, psychoticRabbit wrote:
> On Saturday, 17 March 2018 at 09:18:13 UTC, Nick Treleaven 
> wrote:
>> It's a language design decision as to whether a particular 
>> feature is worth supporting. I would like this feature too 
>> though. I'm not sure how much compiler complexity would be 
>> added by having another visibility modifier.
>>
>
> D could add an new attribute to class members:  @deny
>
> A @deny attribute can come before a classes private member, to 
> indicate that the private member is to remain private, even 
> within the module.
>
> Cause sure, it nice to be among friends, but you don't want 
> your friends knowing every thought that is going through your 
> mind! Sometimes, somethings, just need to remain private.
>
> @deny private string _userName;
>
> now... _userName is no longer accessible at the module level, 
> and class encapsulation is restored.
>
> If had I any clue about compilers, I'd think this through more 
> ;-)

I don't like the name @deny, personally I would rather see the 
private attribute changed to something like:

private(true) // The member is oly visible to its parent.

private(false) // Same as just "private", visible to whole module.

Could be specialized to something like:

private(this) // Same as private(true)

private(module) // Same as private(false)

That way it can be introduced without breaking changes and looks 
cleaner since it wouldn't be yet another attribute.

Really it's just an extension to the already existing attribute.

This feature should be relatively easy to implement since it's 
similar to the "package" attribute, which also takes a value as 
the module name.


More information about the Digitalmars-d-learn mailing list