Thank you!
Steven Schveighoffer
schveiguy at gmail.com
Wed Sep 8 11:22:20 UTC 2021
On 9/8/21 3:25 AM, Mike Parker wrote:
> On Wednesday, 8 September 2021 at 06:03:49 UTC, Tejas wrote:
>
>> Please allow this to compile.
>> `int` is value type, so it is fine to generate a temporary and allow
>> the code to compile, right? A small test will reveal that `integer` is
>> not being modified, so it's not **that** big of a deal, right?
>
> There's a big difference between int and struct: the latter is an
> aggregate that may have member functions that have side effects. For
> example:
>
> ```d
> getTemporary().doSomethingWithSideEffects();
> ```
>
> That's a valid pattern. You might then say that's a different case than
> the assignment in your example, but `opAssign` for a struct can be
> overridden.
>
But in this case, there is no side effect, and the compiler knows it.
TBH, I thought this was already disallowed, but run.dlang.io shows it as
always succeeding.
One possible explanation is for generic code, but I don't buy that
actually, because other rvalue/lvalue differences can cause generic code
to not compile.
I would say disallowing assigning a non-elaborate field (one that is
just a field, and does not call some arbitrary opAssign) would be a
valid enhancement.
-Steve
More information about the Digitalmars-d
mailing list