copy constructors need documentation

John Colvin john.loughran.colvin at gmail.com
Tue Aug 6 09:42:09 UTC 2019


On Tuesday, 6 August 2019 at 08:35:29 UTC, Olivier FAURE wrote:
> On Tuesday, 6 August 2019 at 07:53:25 UTC, RazvanN wrote:
>> No. typeof(this) is going to be immutable(T) and `this` cannot 
>> be modified
>> as in `this = something`, but the first assignment of each of 
>> this's field
>> is considered initialization and allowed:
>>
>> struct T
>> {
>>     int a;
>>     this(int a) immutable
>>     {
>>         pragma(msg, typeof(this));    // immutable(T)
>>         this.a = a;                   // ok, even though 
>> this.a is immutable
>>     }
>> }
>
> Makes sense.
>
> By the way, weird edge cases like that are why I like Rust's 
> semantics (there's no constructor, just struct initialization 
> and factory functions).
>
> Too bad there's no non-painful way to implement them in D at 
> this point.

Can you not just write trivial constructors (or no constructor, 
if struct literals are good enough for the case in question) and 
then use factory functions if you want behaviour like that?


More information about the Digitalmars-d mailing list