DIP60: @nogc attribute

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Thu Apr 24 11:19:05 PDT 2014


On 4/24/2014 6:35 AM, bearophile wrote:
> Currently this code doesn't compile because the lambda allocates the closure on
> the heap:

Pointing out these issues is exactly what @nogc is designed to do.

> void main() @nogc {
>      import std.algorithm: map;
>      int[3] data = [1, 2, 3];
>      immutable int x = 3;
>      auto result = data[].map!(y => y * x);
> }
>
>
> test.d(1,6): Error: function D main @nogc function allocates a closure with the GC
>
> Such kind of code is common,

True.

> so a good amount of range-based code can't be @nogc.

"Can't" is a bit strong of a word. Needing a workaround that is perhaps a bit 
ugly is more accurate. For your example,

     enum int x = 3;

will solve the issue.




More information about the Digitalmars-d mailing list