Random D geekout

F i L witte2008 at gmail.com
Thu Apr 19 22:19:06 PDT 2012


Denis Shelomovskij wrote:
> A shorter variant:
> ---
> void delegate(string, string)[string] dgs = [
> 	"name" : (key, value) { d.name = value; },
> 	"phone": (key, value) { d.phone = value; },
> 	"age"  : (key, value) { d.age = to!int(value); },
> 	...	// whole bunch of other stuff to
> 		// parse different attributes
> ];

That's a pretty slick example of D's type inference. This example 
is worthy of a reference in the docs somewhere, IMO. Although, 
written to use UFCS of course:

     auto m = line.match("(\w+)\s+(\w+)");

     ...

     "age" : (key, value) { d.age = value.to!int(); }

:D gotta love UFCS!


More information about the Digitalmars-d mailing list