check variable for undefinedness

John Colvin john.loughran.colvin at gmail.com
Thu Dec 26 02:13:35 PST 2013


On Thursday, 26 December 2013 at 09:21:39 UTC, Dfr wrote:
> In Javascript there is 'undefined', i can do something like:
>
> var a;
> if(a === undefined) {  a = [1,2,3] }
>
> How such check can be done in D ?

In D, all variables are initialised to the .init value for their
type when declared. If a is a nullable type that's init value is
null (e.g. a class) then use

if(a is null)

In general it's not good to check for equality with the init
value as, for example, the init value for integers is 0, which
can of course be a valid value itself.

However, there's probably a neater way of approaching the
problem. Can you provide a little more context as to what you're
trying to achieve?


More information about the Digitalmars-d-learn mailing list