Type inference for delegates/lambdas as regular parameters?
Alex Rønne Petersen
xtzgzorex at gmail.com
Wed Feb 29 08:50:10 PST 2012
(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
--
- Alex
More information about the Digitalmars-d
mailing list