Sealed classes - would you want them in D? (v2)

aliak something at something.com
Fri May 18 12:16:55 UTC 2018


On Friday, 18 May 2018 at 12:00:58 UTC, Gheorghe Gabriel wrote:
> On Thursday, 17 May 2018 at 02:32:07 UTC, KingJoffrey wrote:
>> I propose an idea, for discussion (robust discussion even 
>> better ;-)
>>
>> Add an new attribute to class, named 'sealed'.
>>
>> No, not sealed as in Scala.
>>
>> No, not sealed as in C#
>>
>> sealed as in oxford dictionary (close securely, non-porous).
>>
>> when sealed is applied on the class, this means the class is 
>> sealed.
>>
>> the sealed attribute only makes sense within a module, and 
>> affects nothing outside of the module.
>>
>> When sealed is applied to the class, then, interfacing to a 
>> class within a module, from code outside that class - but 
>> still within the module, can now only occur via the published 
>> interface of the class.
>>
>> outside code in the module, can no longer directly access your 
>> private parts!
>>
>> The class is sealed.
>
> I think this code has cleaner sintax:
>
> class A {
>     private int x;
>     sealed int y;
> }
> void main() {
>     A a = new A();
>     a.x = 7; // ok, it's private to module
>     a.y = 3; // error, it's sealed to class
> }

You may not need a new word at all. You can also enhance private 
to take arguments. Package already does this. You can give 
private a symbol list that says which symbols this is private 
for. So:

class A {
   private int x;
   private(A) int y;
}
void main() {
   A a = new A();
   a.x = 7; // ok, it's private to module
   a.y = 3; // error, it's sealed to class
}

Cheers,
- Ali




More information about the Digitalmars-d mailing list