Alias delegates and @nogc

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 18 11:50:42 PST 2015


On 2/18/15 11:09 AM, Ivan Timokhin wrote:
> With dmd 2.066.1, this compiles:
>
> void bar(scope int delegate() a) @nogc {}
>
> void foo(int x) @nogc
> {
>      bar(() => x);
> }
>
> but this doesn't:
>
> void bar(alias a)() {}
>
> void foo(int x) @nogc
> {
>      bar!(() => x)();
> }
>
> Fails with
> Error: function test.foo @nogc function allocates a closure with the GC
>
> Is there any way to pass a delegate that:
> 1. avoids indirect calls (like alias);
> 2. does not allocate (like scope delegate);
> 3. captures local variables?
>

Building on ketmar's point, I think you should not worry about tricking 
the compiler into avoiding indirect calls. The compiler could and should 
avoid indirect calls and GC closures when it can do so. In particular, 
it should be able to inline bar, and (I assume in a full version of the 
code) any uses of the delegate.

But it just doesn't yet.

-Steve


More information about the Digitalmars-d-learn mailing list