Local variable inside delegate literal

bearophile bearophileHUGS at lycos.com
Wed Dec 23 00:19:24 PST 2009


Daniel:

> I'm writing some gui code, and am currently trying to make something equivalent to this:
> 
> int delegate()[] funcs;
> funcs.length = 3;
> 
> foreach(i, ref f; funcs)
> {
>   f = int() { return i; }
> }
> 
> foreach(f; funcs)
> {
>   writeln(f());
> }

This is D2 code that actually compiles:

import std.stdio: writeln;

void main() {
    auto funcs = new int delegate()[3];

    foreach (i, ref f; funcs)
        f = { return cast(int)i; };

    foreach (f; funcs)
        writeln(f());
}


This is a good version in D2:

import std.stdio: writeln;

class Foo {
    int i;
    this(int i) { this.i = i; }
    int opCall() { return this.i; }
}

void main() {
    auto funcs = new Foo[3];

    foreach (i, ref f; funcs)
        f = new Foo(i);

    foreach (f; funcs)
        writeln(f());
}


Or even cheaper:

import std.stdio: writeln;

struct Foo {
    int i;
    this(int i) { this.i = i; }
    int opCall() { return this.i; }
}

void main() {
    auto funcs = new Foo[3];

    foreach (i, ref f; funcs)
        f = Foo(i);

    foreach (f; funcs)
        writeln(f());
}

Often the better code is not the most compact one, but the most explicit one.

Bye,
bearophile



More information about the Digitalmars-d mailing list