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!

Reply via email to