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

Reply via email to