How to hand in a closure variable

Matej Nanut matejnanut at gmail.com
Mon Mar 24 10:03:09 PDT 2014


Hello!

You just missed the syntax a little.

Instead of:

> int delegate(int) dg = { value => return value + a + 3; };

You can write

    auto dg = (int value) { return value + a + 3; }; // Omitted return
type, but had to specify type of value.

or

    auto dg = (int value) => value + a + 3; // Notice no "return" keyword.

or

    int delegate(int) dg = value => value + a + 3; // Omitted type of
value, but had to write the full type of dg.

You can also write a delegate as an inner function:

    int a = 7;
    int dg(int value)
    {
        return value + a + 3;
    }
    auto result = dg(123);

I'm not sure, but I guess all of these should mean the same thing.


More information about the Digitalmars-d-learn mailing list