digitalmars.D.learn - check variable for undefinedness
- Dfr (4/4) Dec 26 2013 In Javascript there is 'undefined', i can do something like:
- John Colvin (11/15) Dec 26 2013 In D, all variables are initialised to the .init value for their
- Dfr (23/40) Dec 26 2013 Thank you for reply.
- Rikki Cattermole (15/37) Dec 26 2013 You are correct in that checking if its null will work.
- Jakob Ovrum (25/39) Dec 26 2013 Use a module constructor:
- Gary Willoughby (6/15) Dec 26 2013 It might not be entirely appropriate for this situation and the
- Suliman (1/2) Dec 11 2015 How to check if variable "is not null" ?
- Nicholas Wilson (4/6) Dec 11 2015 a !is null
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
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
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
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
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
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:
It allows a function to cache calculated values. It's pretty neat!
Dec 26 2013
if(a is null)How to check if variable "is not null" ?
Dec 11 2015
On Saturday, 12 December 2015 at 07:39:47 UTC, Suliman wrote:a !is null or !(a is null)if(a is null)How to check if variable "is not null" ?
Dec 11 2015









"Rikki Cattermole" <alphaglosined gmail.com> 