<div dir="ltr">To avoid the delegate being GC allocated, use `scope foo = (int i) { ... }` at call site.<br>You can also make your function signature as `void func(scope void delegate() dg)` in which case it won't allocate if you pass a literal directly.<br></div><div class="gmail_extra"><br><div class="gmail_quote">2016-06-09 23:57 GMT+02:00 maik klein via Digitalmars-d <span dir="ltr"><<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Thursday, 9 June 2016 at 21:32:33 UTC, Alex Parrill wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thursday, 9 June 2016 at 21:02:26 UTC, maik klein wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Has this been done before?<br>
</blockquote>
<br>
Well, yes, the entire point of delegates is to be able to capture variables (as opposed to function pointers, which cannot).<br>
<br>
<br>
auto createADelegate(int captured) {<br>
        return (int a) => captured + a;<br>
}<br>
<br>
void main() {<br>
        auto dg1 = createADelegate(5);<br>
        auto dg2 = createADelegate(32);<br>
        assert(dg1(5) == 10);<br>
        assert(dg1(10) == 15);<br>
        assert(dg2(8) == 40);<br>
        assert(dg2(32) == 64);<br>
}<br>
<br>
<a href="https://dpaste.dzfl.pl/90ebc29651f6" rel="noreferrer" target="_blank">https://dpaste.dzfl.pl/90ebc29651f6</a><br>
<br>
(Unfortunately template delegates, like the ones used with map, don't keep their captured variables alive after the captured variables go out of scope, but it doesn't sound like you need those)<br>
</blockquote>
<br></div></div>
I meant, "has this been implement as a library before". I am well aware that delegates exist in the language but as far as I know you can not do manual allocation with delegates (to avoid the GC).<br>
</blockquote></div><br></div>