Can we fix this?

jfondren julian.fondren at gmail.com
Wed Sep 29 22:04:33 UTC 2021


On Wednesday, 29 September 2021 at 16:47:23 UTC, Steven 
Schveighoffer wrote:
> Some imagined syntax?
>
> ```d
> dgList ~= (@capture i) {writeln(i + 2);};
> // or:
> dgList ~= (@capture i) => writeln(i + 2);
> ```

or,

"Fix this bug or I'll write this code."

```d
string closure(string vars, string dg) {
     import std.string : split;

     string res = "{\n";
     foreach (var; vars.split(" ")) {
         res ~= "    auto " ~ var ~ " = " ~ var ~ ";\n";
     }
     res ~= "    return " ~ dg ~ ";\n}()";
     return res;
}

void main() {
     import std.stdio : writeln;

     void delegate()[] list;

     foreach (int i; [1, 2, 3]) {
         list ~= mixin(`i`.closure(`{ writeln(i); }`));
     }
     foreach (int i; [1, 2, 3]) {
         auto b = i + 2;
         list ~= mixin(/+ oops: +/`i`.closure(`{ writeln(b); }`));
     }

     foreach (dg; list)
         dg(); // output: 1 2 3 5 5 5
}
```

https://en.wikipedia.org/wiki/If_You_Don't_Buy_This_Book,_We'll_Kill_This_Dog!


More information about the Digitalmars-d mailing list