On Tuesday, 19 August 2014 at 14:13:38 UTC, Andrei Alexandrescu wrote:
On 8/19/14, 12:25 AM, ketmar via Digitalmars-d wrote:
of course, you'll loose such nice features as closures and
slices, but
hey, C++ doesn't have them too! ok, C++11 has lambdas, and i don't know
if D lambdas can work without GC and don't leak.

They don't use GC if scoped.

Andrei

And, in 2.066, it works with @nogc. Scoped no-gc downward closures.

alias dgFloatToFloat = float delegate(float) @nogc;
alias dgFloatPairToFloat = float delegate(float, float) @nogc;

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

float integrate(scope dgFloatPairToFloat f,
                float x0, float x1, size_t nx,
                float y0, float y1, size_t ny) @nogc {
  return integrate((y) => integrate((x) => f(x,y), x0, x1, nx),
                   y0, y1, ny);
}

I was going to ask for an @nogc { <fundefs> } to reduce the noise, but I just tried it and it seems to work. Nice!

Reply via email to