Sealed classes - would you want them in D?

Piotr Mitana the.mail.of.mi2 at gmail.com
Mon May 14 08:32:19 UTC 2018


On Saturday, 12 May 2018 at 15:36:56 UTC, Walter Bright wrote:
> On 5/12/2018 8:18 AM, Piotr Mitana wrote:
>> What I am trying to do is:
>
>
> ========== a.d ============
>
> class P
> {
>     private this();
> }
>
> final class C1 : P { }
> final class C2 : P { }
>
> ======== test.d ============
>
> import a;
>
> void foo()
> {
>     P[] twoPs = [ new C1(), new C2() ]; // works
> }
>
> class C3 : P   // fails
> {
> }
>
> ============================
>
> dmd test a
> Error: constructor a.P.this is not accessible from module test

OK, Walter, point for you for this :)

I still think that this is a kind of a hack (if I need to create 
an instance of P directly, I need a factory method - it's also 
not that clean and obvious), but in fact it effectively seems to 
do what I want to achieve.

Although I consider sealed as a nice addition still to make 
things nice and clean, I understand that it may be not enough 
justified to add a new keyword to the language. Thank you for all 
the opinions on that. :)


More information about the Digitalmars-d mailing list