Sealed classes - would you want them in D?

Mike Parker aldacron at gmail.com
Tue May 15 14:07:41 UTC 2018


On Tuesday, 15 May 2018 at 10:19:58 UTC, KingJoffrey wrote:

> Actually, I kinda get it for unit tests.
>
> Surely there's more??

I use it all the time. One way is to replace what would have been 
a "Manager" class in Java or elsewhere.

module foo.window;
class Window {
     private WindowHandle _handle;
     private void close() {}
}

private Window[WindowHandle] _windows;
package void closeWindow(Window window) {
     _windows.remove(window._handle);
     _window.close();
}

This is exactly how I reason about the code and is similar to how 
I would have structured it in C, but with the benefit of both 
package- and module-level encapsulation. _windows, 
window._handle, and window.close are hidden from both the outside 
world and the package.

I can't see any possible reason why I would want to hide the 
implementation of Window from closeWindow -- they're in the same 
module! It's not like C++, where multiple classes can belong to 
multiple namespaces in the same source file. Everything in 
window.d is in the same namespace.

>
> 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?

D doesn't make that exemption. No one from outside the module has 
access to your private parts. If you have access to the class, 
you also have access to the module, and everything is in the same 
namespace by design, so you're grabbing your own private parts. 
Why should programmers be prevented from doing that?





More information about the Digitalmars-d mailing list