D idioms list

Foo via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Thu Jan 8 16:26:42 PST 2015


I saw recently (at last in this thread: 
http://forum.dlang.org/thread/tdfydchrairigdlgtyum@forum.dlang.org#post-qakiogaqvmiwlneimhgu:40forum.dlang.org) 
that many users use
----
key in aa ? aa[key] : ValueType.init;
----
instead of
----
auto ptr = key in aa;
ptr ? *ptr : ValueType.init;
----
which is more economic.
Maybe you can add it to your list:

----
import std.stdio;

void main() {
	immutable string key = "foo";
	immutable string[string] arr = [key : "bar"];
	
	if (auto ptr = key in arr)
		writeln(*ptr);
}
----


More information about the Digitalmars-d-announce mailing list