Is variable void?

codephantom me at noyb.com
Mon Nov 27 02:12:40 UTC 2017


On Saturday, 25 November 2017 at 15:34:21 UTC, John Chapman wrote:
> Is there any way of determining whether a variable has been 
> initialized or not? For example, if something is declared like 
> this:
>
>   int x = void;
>
> can I check if it's void before I use it, say, in a function 
> it's been passed to?

// ----------------------------------

module test;

import std.stdio;
import std.typecons; // see: 
https://dlang.org/phobos/std_typecons.html#Nullable

void main()
{
     Nullable!int x;  // requires: import std.typecons
     assert(x.isNull);
     writeln("x is ", x);

     x = 1;
     assert(!x.isNull);
     writeln("x is ", x);

     x.nullify(); //  	Forces x back to a null state.
     assert(x.isNull);
     writeln("x is ", x);

}
// ----------------------------------



More information about the Digitalmars-d-learn mailing list