How to avoid inout type constructor with Optional type wrapper undoing string type
Steven Schveighoffer
schveiguy at gmail.com
Fri Jul 27 14:52:20 UTC 2018
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
More information about the Digitalmars-d-learn
mailing list