Extending D's support for object-oriented design with private(this)
Dom DiSc
dominikus at scherkl.de
Tue Apr 30 15:21:27 UTC 2024
On Tuesday, 30 April 2024 at 00:01:00 UTC, NotYouAgain wrote:
> The source of all this, can be trace back to the D designer who
> decided to change the "right and obvious" meaning of private.
>
> That was a mistake. It's not that the module private is wrong,
> that certainly has a place, but changing the meaning of
> private, was a mistake that D has to live with.
It's not "wrong". It's just a different philosophy, like with
transitive const.
You can claim something is "logical const" but still modify
internal state, or you enforce that something that is claimed
const can really not be modified whatsoever.
Neither of the two is "wrong", it's just different and you can
not easily translate between the two.
With private you can claim something is not accessed by other
stuff within the same file (by declaring it "private to the
class"), but this is only "logical private", because effectively
if you have access to a file, you can access everything in that
file, no matter how "private" it is.
But if something is in a different file, you can enforce that
someone cannot access (aka modify) what is in there. Maybe simply
by making the file readonly, but there are also stronger
protection measures possible.
As I said in the other thread: A black-box test (for example
something that tests the encapsulation of an object) shall not
have the possibility to modify the object under test. So it
cannot be in the same file. If you implement it in the same file
and claim privacy, you are lying to yourself. It is simply not
true.
In both cases (const and private) C++ allows the programmer to
lie to himself (which is convenient, I confess) and D enforce
correct behaviour (which is inconvenient at times).
I prefer the D philosophy.
I have nothing against private(this), it's convenient, you are
right.
But I hate it if someone insist to call the other philosophy
"wrong". That is what provoces so much resistence in these
threads. Your chances to get the feature you want so much will
become much higher, if you refreign from insulting others.
More information about the dip.ideas
mailing list