readonly member (but assignable at constructor time)

bauss jj_1337 at live.dk
Mon Apr 30 10:36:52 UTC 2018


On Saturday, 28 April 2018 at 04:56:26 UTC, lempiji wrote:
> On Friday, 27 April 2018 at 02:59:16 UTC, Dr.No wrote:
>> In C# you can have a readonly member assignable either at 
>> declaration or constructor time, like this:
>>
>> class C
>> {
>>   readonly myClass mc;
>>
>>   this()
>>   {
>>     mc = new myClass();
>>   }
>>
>>
>>   void doSomething()
>>   {
>>     mc = new myClass(); // wrong! result in compiler error, mc 
>> is readonly
>>   }
>> }
>>
>> Does D have something like this natively or there's a way to 
>> do so with traits/CTFE at runtime?
>
> You can use std.experimenta.typecons.Final.
> https://dlang.org/phobos/std_experimental_typecons.html#.Final

I don't know why D always swears to have library solutions for 
everything.

This is clearly something that should have a DIP and a language 
implementation in my opinion, considering it's a type-system 
addition.

It looks like a bad hack honestly.


More information about the Digitalmars-d-learn mailing list