www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Test if variable has void value

reply Andrey <saasecondbox yandex.ru> writes:
Hello,
How to test if variable has void value?

 string text = void;
if(text == void)
{
    writeln("Is void");
}
Tried this:
if(is(text == void))
but doesn't work.
Aug 22 2018
parent Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Wednesday, 22 August 2018 at 13:50:07 UTC, Andrey wrote:
 Hello,
 How to test if variable has void value?

 string text = void;
if(text == void)
{
    writeln("Is void");
}
Tried this:
if(is(text == void))
but doesn't work.
You can't. When using a void initializer the actual value is garbage until initialized properly, and that garbage can look like anything including a valid instance. So if the flow of your program can't guarantee that the value has been initialized at a certain point, you'll have to track it yourself some way. Nullable may be of help: https://dlang.org/phobos/std_typecons.html#Nullable
Aug 22 2018