On Tuesday, 27 March 2018 at 15:37:11 UTC, SimonN wrote:
On Tuesday, 27 March 2018 at 15:28:40 UTC, jmh530 wrote:
        static if (isMutable!T)
            bag[0] = rhs;
        else
            bag = [rhs];

I like this idea. I'd even take it a step futher:

When T is a pointer or class reference, then we can put the reference on the stack (instead of into the array) and handle assignments like Rebindable handles assignments -- provided that Rebindable really is 100 % safe to the outside, see my concerns from 2 posts above. In this case (static if), we won't even declare the array T[] bag, and instead implement as T value, bool isPresent.

When T is a mutable value type, it goes on the stack, too. Again no array.

When T is a const/immutable/inout value type, we declare the array as before and rebind on assignment with bag = [rhs], as you proposed here.

-- Simon

I think this all sounds good. I found the "magic" as well in Seb's Rebindable PR link for value types so might be able to have stack storage for const value types as well [1]. Not sure how safe it is.

Inout poses a problem in all cases though and I'm unsure how to solve it without stripping the qualifiers completely from the payload. Basically:

struct S(T) {
    T t;
}

auto make(T)(T t) {
    return S!(T)(t);
}

class C {
    int i;
    auto f() inout {
        make(i);
    }
}

Will not compile.

I can make T an Unqual!T, but then we are open to type breakage in cases where users should have access to member t.


[1] https://github.com/dlang/phobos/pull/6136/files#diff-4e008aedb3026d4a84f58323e53bf017R2223

Reply via email to