How Nested Functions Work, part 1
bearophile
bearophileHUGS at lycos.com
Wed Sep 2 05:00:19 PDT 2009
Nick Sabalausky:
> Quick! For each of these, valid or invalid?:
I may like this one:
import std.stdio: writeln;
import d.templates: IsCallable;
void repeat(TyCall)(int n, TyCall callme) if (IsCallable!TyCall) {
foreach (i; 0 .. n)
callme(i);
}
void main() {
repeat(5, (int i){ writeln("hi"); });
}
It works if callme is a delegate, function pointer or callable object, and gives good error messages otherwise.
The bad thing is that currently the LDC compiler has not enough semantics to inline that delegate. If you need more performance you can use the template trick(s) used by std.algorithm of Phobos2.
Bye,
bearophile
More information about the Digitalmars-d
mailing list