Is this really intended??
claptrap
clap at trap.com
Sun Oct 11 21:50:28 UTC 2020
On Sunday, 11 October 2020 at 18:57:55 UTC, Steven Schveighoffer
wrote:
> On 10/10/20 8:16 PM, claptrap wrote:
>> struct Foo
>> {
>> int i;
>> void opAssign(int rhs) { this.i = rhs; }
>> void reset() { i = 0; }
>> }
>>
>> class Bar
>> {
>> Foo foo;
>>
>> this()
>> {
>> foo.i = 0; // *1
>> foo.reset(); // *2
>> foo = 42;
>> }
>> }
>>
>> With *1 & *2 commented out...
>>
>> onlineapp.d(19): Error: cannot implicitly convert expression x
>> of type int to Foo
>> onlineapp.d(19): this.foo = x is the first assignment
>> of this.foo therefore it represents its initialization
>> onlineapp.d(19): opAssign methods are not used for
>> initialization, but for subsequent assignments
>
> Hm.. yeah, this seems not right. The compiler should just go
> ahead and initialize it if you try to call any methods on it
> (including opAssign) before initialization.
I think the struct should be initialised full stop. I mean
whether a member is initialised before the constructor is run
shouldn't be dependent on what you do in the constructor. I mean
you declare a struct, whether local or as part of an aggregate,
it should be default initialised. Its a simple orthogonal rule.
If it's to do with being able to modify immutable members in the
constructor that should be a separate rule. Either "You can
modify immutable members in a constructor", or "you can construct
immutable members in a constructor, but only once and it must be
the first action on the member"
If there's a question of wasted effort initialising a member
twice, the compiler should be able to elide the default
initialiser, at least in simple cases the mechanism is already
there.
They should be separate orthogonal rules.
More information about the Digitalmars-d
mailing list