Random D geekout

Ali Çehreli acehreli at yahoo.com
Fri Apr 20 12:00:06 PDT 2012


On 04/20/2012 11:50 AM, Arne wrote:
> On Friday, 20 April 2012 at 11:23:49 UTC, Steven Schveighoffer wrote:
>> On Fri, 20 Apr 2012 00:06:41 -0400, H. S. Teoh
>> <hsteoh at quickfur.ath.cx> wrote:
>>
>>
>>> The only complaint is that I couldn't write auto[string] dgs and have
>>> the compiler auto-infer the delegate type. :-)
>>
>> Does this not work?
>>
>> auto dgs = ...
>>
>> Also, it doesn't look like that needs to be in the inner loop. Each
>> time you specify an AA literal, it allocates a new one. So you are
>> allocating another AA literal per line.
>>
>> -Steve
>
> auto dgs =
> [
> "name": (string value) {d.name = value; },
> "phone": (string value) => cast(void)(d.phone = value),
> "age": (string value) => cast(void)(d.age = value.to!int()),
> ];
>
> This works... is there a better way, to avoid cast?
>

The => syntax replaces:

- parentheses around the parameter if there is only one parameter
- curly brackets
- the return keyword
- the semicolon at the end of the return statement

   http://dlang.org/expression.html#Lambda

So => is most suitable when there is a single return statement.

Ali


More information about the Digitalmars-d mailing list