www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Insert if doesn't exist without double lookup.

reply "Agustin" <wolftein1 gmail.com> writes:
Hello!, i would like to know if this is possible.

auto asValue ?= (map["Key"] == new Value);

Instead of doing:

if (("Key" in map) is null)
    map["Key"] = new Value
auto asValue = map["Key"];
Mar 15 2014
parent Max Klyga <max.klyga gmail.com> writes:
On 2014-03-15 18:44:46 +0000, Agustin said:

 Hello!, i would like to know if this is possible.
 
 auto asValue ?= (map["Key"] == new Value);
 
 Instead of doing:
 
 if (("Key" in map) is null)
     map["Key"] = new Value
 auto asValue = map["Key"];
auto needle = "Key" in map; auto value = needle ? *needle : (map["Key"] = new Value);
Mar 15 2014