Is this really intended??
Paul Backus
snarwin at gmail.com
Sun Oct 11 00:34:57 UTC 2020
On Sunday, 11 October 2020 at 00:16:53 UTC, claptrap wrote:
> Arn't structs supposed to be default initialised?
>
> But in a constructor a struct member is not? If you write to a
> field in a struct member suddenly it is considered initialised?
>
> And yet it's OK to call a method on the struct that is not
> initialised, but if you do it's still uninitialised?
>
> Why should
>
> foo.i = 0;
> foo.reset();
>
> Result in the compiler changing whether it's OK to call
> opAssign?
The first assignment to a member inside a constructor is
considered initialization so that you can use constructors to
initialize immutable members:
struct Example
{
immutable int i;
this(int i)
{
this.i = i; // ok, initialization
this.i = 42; // error, assignment to immutable variable
}
}
One consequence of this is that the first assignment does not
call opAssign, since opAssign isn't used for initialization.
I agree that this is kind of a hack. A more principled way to
handle this would be to introduce a separate syntax for
initialization, like `let this.i = i` or `this.i := i`.
More information about the Digitalmars-d
mailing list