Sealed classes - would you want them in D?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat May 12 07:39:04 UTC 2018


On Saturday, May 12, 2018 07:29:47 KingJoffrey via Digitalmars-d wrote:
> On Saturday, 12 May 2018 at 07:19:47 UTC, rikki cattermole wrote:
> > I see no problem.
> >
> > onlineapp.d(1): Error: label wtf is undefined
>
> The 'Error' is my point. It's not possible to do this - which is
> a good thing.
>
> D protects the encapsulation unit of the function from such abuse.
>
> But the same is not true for classes.

You could argue the same of friend functions in C++.

Yes, D weakens the encapsulation of classes in comparison to C++, but it
simplifies the language in comparison to needing friend functions like C++
does, and it works _really_ well with stuff like unit testing. Other
languages are forced to do stuff like have member functions and variables be
protected just so that you can have a unit testing class access them via
inheritance, whereas D avoids the need to change the class' API just to be
able to test it, because the module has full access to it.

Ultimately, it's a tradeoff, and arguments can be made for and against. But
in practice, it works extremely well. You're certainly free to not like this
particular design choice, but it's one that most of us have no problem with,
and I'd be shocked to ever see it change. So, you can be unhappy about it,
but complaining isn't going to change anything. You're either going to have
to just learn to live with it or not use D.

- Jonathan M Davis



More information about the Digitalmars-d mailing list