Sealed classes - would you want them in D?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri May 11 23:28:48 UTC 2018


On Friday, May 11, 2018 16:14:43 Walter Bright via Digitalmars-d wrote:
> On 5/10/2018 6:22 AM, Piotr Mitana wrote:
> > For those who never coded Scala and don't know sealed classes: a sealed
> > class is a class which can be only extended in the same source file.
> >
> >      sealed class MyClass {}
> >
> > Translating to D, a sealed class would could only be extended in the
> > same
> > module.
>
>    private class MyClass { }
>
> should do the trick.

Except that if I understand correctly, what the OP wants is to have the
class be publicly available while restricting who's allowed to derive from
it, with the idea that a particular class hierarchy would have a
well-defined set of classes that the person who wrote them had full control
over rather than allowing anyone and everyone to derive from any class in
the hierarchy except for any final classes at the leaves of the hierarchy.
So, you have multiple classes deriving from each other and have them all be
public but not don't allow other code to derive from any them. It would
arguably be useful for any situation where you want to make a class
hierarchy available without letting it be extendable. Whether that's worth
adding a feature to the language in order to get, I don't know, but I don't
see how it would be possible to achieve with the language as-is.

- Jonathan M Davis



More information about the Digitalmars-d mailing list