Sealed classes - would you want them in D?

KingJoffrey KingJoffrey at KingJoffrey.com
Tue May 15 10:19:58 UTC 2018


On Tuesday, 15 May 2018 at 07:23:55 UTC, Jonathan M Davis wrote:
>
> The prime one is unit tests. The fact that they can access the 
> private variables is invaluable for testing the state of an 
> object. In C++, I have always have to make all of the tests 
> friends of the class so that they can access the internals for 
> testing. In D, I don't have to worry about any of that, and the 
> tests don't affect the public interface of the class at all.

ok. so the 'for' side arguments, for embracing Facebook style 
friendship in D:

- Voldemort types (cause yeah...we all use them so often).
- Unit tests (dare I the same ;-)

Actually, I kinda get it for unit tests.

Surely there's more??


> The assumption is that anything that goes in the same module 
> can either reasonably be treated as friends, or it doesn't 
> matter if they are.

Well, we already know what disasters can occur from facebook 
style friendships.

My own code in D had bugs, cause I didn't realise all my private 
parts were not just visible, but accessible by all the so called 
'friends' around me. They could reach in a do whatever they want! 
Without my consent!

And btw, people grabbing your private parts without your explicit 
consent, is a really serious matter! Just ask those in the me-too 
movement.

Why should a programmers code be exempt from such considerations?

> Basically all of the same arguments that you're giving against 
> having everything in the module being treated as friends of 
> each other can be given against having friends in the first 
> place. It's just that D makes it implicit within the module.
>

No. In C++, you only declare friends as part of your defined 
interface.

That's kind different to 'everyone around you is your friend. so 
you need to deal with it'.

They are not the same thing, and have to be approached 
differently.



More information about the Digitalmars-d mailing list