Extending D's support for object-oriented design with private(this)
NotYouAgain
NotYouAgain at gmail.com
Sat Apr 27 05:50:33 UTC 2024
On Saturday, 27 April 2024 at 02:03:35 UTC, Richard (Rikki)
Andrew Cattermole wrote:
> On 27/04/2024 11:50 AM, NotYouAgain wrote:
>> On Friday, 26 April 2024 at 16:38:54 UTC, Steven Schveighoffer
>> wrote:
>>> ...
>>> module m;
>>>
>>> class C
>>> {
>>> private(this) int _count;
>>> public void setCount(int c) { _count = c; }
>>>
>>> unittest
>>> {
>>> C c = new C();
>>> c._count = 42; // oops, this is still allowed
>>> }
>>> }
>>> ```
>>
>> There is no 'oops' there.
>>
>> The unittest is a member of the class!
>>
>> It the unittest outside of the class that should not be
>> allowed to compile.
>
> Based on the fact that a unittest block does not have a this
> pointer, Steven is correct. It isn't part of the scope.
>
> If you want this you have to be specific that it is included
> (and yes this is an example of why a DIP is needed to know what
> your assumptions are).
No I don't understand.
The unittest is a special function being declared inside the
scope of the class type. I would entirely expect it to have
access to all private members of that type.
When the unittest is declared outside the scope of the function,
I would expect it to NOT have access to the private(this) members
of the type.
That's exactly what I expect and exactly how it works in OpenD.
I presume it's a static function? Hence no this pointer? (I
really don't know how its implemented, nor do I care. I can only
care that it works as I expect it to work in OpenD.
More information about the dip.ideas
mailing list