Given the `struct S` with lots of data fields, I've written the following functional way of initializing only a subset of the members in an instance of `S`:

struct S
{
    int i;
    float f;
    ...

    this(int i) { this.i = i; }

    S withF(float f)
    {
        // will this be optimized out if `this` is an r-value?
        S copy = this;
        copy.f = f;
        return copy;
    }
}

Now the question becomes: will the S-copying inside `withF` be optimized out in the case when `this` is an r-value such as in the call

    auto y = S(32).withF(42.0);

?

If not, one solution of doing this manually is to write `withF` as a free function

S withF()(auto ref S this_, float f)
{
    static if (__traits(isRef, this_))
    {
        // this_ is an l-value (and was passed by ref)
        // so a copy has to be made before modifying it
    }
    else
    {
        // this_ is an r-value (and was passed by move)
        // so it can be modified in-place
    }
}

Is this the preferred way of solving this until we (ever) get named parameters in D?

Reply via email to