[dmd-internals] Type inference for delegates/lambdas as regular parameters?

Alex xtzgzorex at gmail.com
Wed Feb 15 15:24:45 PST 2012


Hi,

Consider this code:

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; the type of x in the lambda
expression cannot be deduced. Specifying the type explicitly works
fine.

This works:

bool contains(alias dg, T)(T[] arr)
{
    foreach (i; arr)
        if (dg(i))
            return true;

    return false;
}

import std.stdio;

void main()
{
    writeln(contains!(x => x == 2)([1, 2, 3]));
}

Wasn't there supposed to be type inference for delegates passed as
regular parameters in 2.058?

Regards,
Alex


More information about the dmd-internals mailing list