www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - check variable for undefinedness

reply "Dfr" <deflexor yandex.ru> writes:
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 ?
Dec 26 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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?
Dec 26 2013
next sibling parent reply "Dfr" <deflexor yandex.ru> writes:
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?
Dec 26 2013
next sibling parent "Rikki Cattermole" <alphaglosined gmail.com> writes:
On Thursday, 26 December 2013 at 11:03:17 UTC, Dfr wrote:
 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;
   }
   ....
 }
You are correct in that checking if its null will work. Another alternative is to check the length of the keys array. if (b.keys.length == 0) { // blah something } Are you aware of the static this feature? static this() { if(b is null) { b = a.map!(...).assocArray; } } It runs on init of the module. In other words at the start of the application (before main). Although in this case you probably don't need to check if its null. As it can be assumed.
Dec 26 2013
prev sibling next sibling parent "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Thursday, 26 December 2013 at 11:03:17 UTC, Dfr wrote:
 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
Use a module constructor: --- immutable a = [["a","1"], ["b", "2"]]; immutable string[string] b; shared static this() { b = cast(immutable)a.map!(pair => tuple(pair[1], pair[0])).assocArray; } --- Shared module constructors are called once, before the main function. Non-shared module constructors are called once for every thread - on the thread's creation (which, naturally, is before main for the main thread), and should be used to initialize TLS variables. I took the liberty of changing the variables to be `immutable`. When creating new data, `const` makes little sense - choose either mutable or immutable. A module constructor is the only place `immutable` or `const` module-level variables without initalizers as part of their declaration can be initialized. The cast is required because the compiler fails to infer the uniqueness of the initializer expression, but unique inference is a work in progress, so this may (should) be allowed in the future.
Dec 26 2013
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Thursday, 26 December 2013 at 11:03:17 UTC, Dfr wrote:
 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;
   }
   ....
 }
It might not be entirely appropriate for this situation and the module constructor is perhaps the way to go but take a look at this: http://dlang.org/phobos/std_functional.html#.memoize It allows a function to cache calculated values. It's pretty neat!
Dec 26 2013
prev sibling parent reply Suliman <evermind live.ru> writes:
 if(a is null)
How to check if variable "is not null" ?
Dec 11 2015
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 12 December 2015 at 07:39:47 UTC, Suliman wrote:
 if(a is null)
How to check if variable "is not null" ?
a !is null or !(a is null)
Dec 11 2015