Avoid GC with closures

bpr via Digitalmars-d digitalmars-d at puremagic.com
Thu May 26 14:10:30 PDT 2016


On Thursday, 26 May 2016 at 18:53:35 UTC, Iakh wrote:
> Functions with lambdas cannot be @nogc as far as they allocates 
> closures.

Counterexample:

//  Note that this is NOT a good way to do numerical quadrature!

double integrate(scope double delegate(double x) @nogc f,
                  double lo, double hi, size_t n) @nogc {
   double result = 0.0;
   double dx = (hi - lo) / n;
   double dx2 = dx * 0.5;
   for (size_t i = 1; i <= n; i++) {
     result += f(lo + i * dx2) * dx;
   }
   return result;
}

double integrate(scope double delegate(double, double) @nogc f,
                             double x0, double x1,
                             double y0, double y1,
                             size_t nX, size_t nY) @nogc {
   return integrate((y) => integrate((x) => f(x,y), x0, x1, nX), 
y0, y1, nY);
}

Functions with @nogc downward funarg lambdas (delegates) can be 
@nogc.

I can't parse the rest of your post, maybe I misunderstand you.



More information about the Digitalmars-d mailing list