Type inference for delegates/lambdas as regular parameters?

bearophile bearophileHUGS at lycos.com
Wed Feb 29 09:56:32 PST 2012


Alex R. Petersen:

> bool contains(T)(T[] arr, scope bool delegate(T) dg)
> in
> {
>     assert(dg);
> }
> body
> {
>     foreach (i; arr)
>         if (dg(i))
>             return true;
> 
>     return false;
> }
> 
> import std.stdio;
> 
> void main()
> {
>     writeln(contains([1, 2, 3], x => x == 2));
> }
> 
> This doesn't compile with 2.058;

dmd 2.059head fails with this reduced version, I think it's a bug for Bugzilla:


int foo(T)(int delegate(T) del) {
    return del(1);
}
void main() {
    assert(foo((int x) => x + x)); // OK
    assert(foo(x => x + x)); // error
}

Bye,
bearophile


More information about the Digitalmars-d mailing list