Type inference for delegates/lambdas as regular parameters?

Timon Gehr timon.gehr at gmx.ch
Wed Feb 29 09:04:52 PST 2012


On 02/29/2012 05:50 PM, Alex Rønne Petersen wrote:
> (Didn't have much luck posting this on dmd-internals, so I'm posting here.)
>
> 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
>

There is (try instantiating the template explicitly). The issue is that 
IFTI matching is performed independently on every parameter afaik.


More information about the Digitalmars-d mailing list