check variable for undefinedness

Dfr deflexor at yandex.ru
Thu Dec 26 03:03:16 PST 2013


Thank you for reply.

Here is what i trying to achieve, i have module-wise data 
structure, which should exist in form of array and associative 
array, but i can't calculate second form on compile time:

const a = [["a","1"],["b", "2"], ... ];
const string[string] b = a.map!(...).assocArray;

This is not allowed, so i trying this approach:

const a = [["a","1"],["b", "2"], ... ];
const string[string] b;

int some_func() {
   b = a.map!(...).assocArray;
   ....
}

It is ok, but i don't want calculate 'b' every time 'come_func' 
is called,
so i'd like to do something like this:

int some_func() {
   if(b is null) {
      b = a.map!(...).assocArray;
   }
   ....
}




On Thursday, 26 December 2013 at 10:13:36 UTC, John Colvin wrote:
> 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