How to avoid inout type constructor with Optional type wrapper undoing string type
aliak
something at something.com
Sat Jul 28 22:06:01 UTC 2018
On Friday, 27 July 2018 at 14:52:20 UTC, Steven Schveighoffer
wrote:
> On 7/23/18 2:39 PM, aliak wrote:
>> Hi,
>>
>> I'm playing around with an Optional wrapper type. It stores a
>> type T and a bool that defines whether a value is defined or
>> not:
>>
>> struct Optional(T) {
>> T value;
>> bool defined = false;
>> this(U : T)(auto ref inout(U) value) inout {
>> this.value = value;
>> this.defined = true;
>> }
>> }
>
> Don't use inout here. The point of inout on the constructor is
> to *transfer* the mutability of the parameter to the struct
> instance. But you want to simply copy the type into the struct
> (an immutable(Optional!T) is quite useless, no?)
>
> Just use U, not inout(U), and don't put inout on the
> constructor.
>
> -Steve
But then it only works for mutable Optional right? Why would an
immutable(Optional!T) be useless? Data can be "forever" empty or
a certain value.
More information about the Digitalmars-d-learn
mailing list