Test if variable has void value

Rene Zwanenburg renezwanenburg at gmail.com
Wed Aug 22 13:55:26 UTC 2018


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



More information about the Digitalmars-d-learn mailing list