check variable for undefinedness

Gary Willoughby dev at nomad.so
Thu Dec 26 04:09:35 PST 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:

http://dlang.org/phobos/std_functional.html#.memoize

It allows a function to cache calculated values. It's pretty neat!


More information about the Digitalmars-d-learn mailing list