Optional type - how to correctly reset a wrapped immutable T

SimonN eiderdaus at gmail.com
Tue Mar 27 15:37:11 UTC 2018


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


More information about the Digitalmars-d-learn mailing list