Extending D's support for object-oriented design with private(this)

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sat Apr 27 02:03:35 UTC 2024


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).


More information about the dip.ideas mailing list