Thank you!
Tejas
notrealemail at gmail.com
Wed Sep 8 08:27:13 UTC 2021
On Wednesday, 8 September 2021 at 07:25:52 UTC, 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.
And what I'm saying is that the user _doesn't_ know that they're
getting a temporary unless they're aware that `struct`s are value
types. And in this particular case, `opAssign` isn't defined.
So I personally think this warrants a warning at the very least.
I would have 0 problems if the function returned `S(_s)` instead
of `_s`.
But this has gone on long enough for me. Maybe I'm the only one
who finds it unintuitive. Fair enough.
More information about the Digitalmars-d
mailing list