Initialize to None
starcanopy
starcanopy at protonmail.com
Sun Sep 6 05:06:26 UTC 2020
On Sunday, 6 September 2020 at 03:42:36 UTC, N.S. wrote:
> I'd like to check whether a variable is initialized or not. And
> I'd also like to uninitialize a variable that is already
> initialized. Thanks!
>
> int x = void;
>
> if (x == void)
> {
> writeln("x not initialized");
> }
> else
> {
> // OK, use x
> }
>
> // Uninitialize x
> x = void;
You might consider Nullable from std.typecons; however, if you're
avoiding implicit initialization for performance reasons, then
this solution might not be applicable.
import std.stdio: writeln;
import std.typecons: Nullable;
Nullable!int x;
if (x.isNull)
{
writeln("x not 'initialized'");
}
else
{
// OK, use x
}
// 'Uninitialize' x
x.nullify();
More information about the Digitalmars-d-learn
mailing list