On Monday, 30 April 2018 at 10:57:51 UTC, Jonathan M Davis wrote:
On Monday, April 30, 2018 10:36:52 bauss via Digitalmars-d-learn wrote:
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.

Well, once upon a time, early in D2's development, it actually did have both head and tail const, but it was deemed too complicated, and we ended up with the transitive const that we have today that works as tail-const under some circumstances but not all. The fact that immutable exists really complicates things, more or less requiring tranitive const, and the fact that the type system doesn't really distinguish between a class and its reference also complicates things.

Personally, I think that head-const is almost worthless, so I see no real value in adding it, but the fact that we're forced to have Rebindable to get tail-const for classes is defintely ugly, though AFAIK, Walter and Andrei both consider it to be a perfectly acceptable solution given the issues with altering how the language treats classes.

Maybe a solid DIP could be constructed which would be accepted, but it's likely going to need some good arguments given how much this has been argued over in the past.

- Jonathan M Davis

Yeah, honestly I'm in the same opinion as you.

Usually when I have been writing C# and used readonly, what I really wanted to use is immutable.

I'm not saying we need to implement this in the language, but just that the library solution isn't really elegant at all, compared to what a language implementation would have been.

Reply via email to