Optional type - how to correctly reset a wrapped immutable T
jmh530
john.michael.hall at gmail.com
Tue Mar 27 15:28:40 UTC 2018
On Tuesday, 27 March 2018 at 13:51:20 UTC, jmh530 wrote:
>
>
> How about:
> [snip]
I can kind of like this more, but after re-reading your original
post I'm not sure it really resolves your issue:
struct Optional(T) {
import std.traits : isMutable;
T[] bag;
this(T t) inout {
bag = [t];
}
void opAssign(T rhs)
{
static if (isMutable!T)
bag[0] = rhs;
else
bag = [rhs];
}
}
Optional!T optional(T)(T x)
{
return Optional!T(x);
}
void main()
{
int x = 3;
const(int) xx = 3;
immutable(int) xxx = 3;
immutable(int) xxxx = 4;
auto y = optional(x);
auto yy = optional(xx);
auto yyy = optional(xxx);
yyy = xxxx;
}
More information about the Digitalmars-d-learn
mailing list