TDPL: Function literals with missing argument type specifiers do not compile

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Aug 2 19:17:38 PDT 2010


One other thing which is semi-relevant, from TDPL:

auto f = (int i) {};
assert(is(f == function));

f seems to be a delegate by default, not a function.

Andrej Mitrovic Wrote:

> The following example should compile according to TDPL. The compiler is supossed to automatically infer the type of the parameter in the function literal:
> 
> void main() { }
> 
> T[] find(alias pred, T)(T[] input)
>     if (is(typeof(pred(input[0])) == bool))
> {
>     for (; input.length > 0; input = input[1 .. $])
>     {
>         if (pred(input[0]))
>             break;
>     }
>     return input;
> }
> 
> unittest {
>     int[] a = [1, 2, 3, 4, -5, 3, -4];
>     
>     // find first negative number
>     auto b = find!(function bool(int x) { return x < 0; })(a);
>     auto c = find!(function (x) { return x < 0; })(a);
>     auto d = find!((x) { return x < 0; })(a);
> }
> 
> The auto c and auto d lines are not compiling:
> higher_functions.d(55): Error: template higher_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) does not match any function template declaration
> higher_functions.d(55): Error: template higher_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) cannot deduce template function from argument types !(__funcliteral2)(int[])
> higher_functions.d(55): Error: template instance errors instantiating template
> higher_functions.d(56): Error: template higher_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) does not match any function template declaration
> higher_functions.d(56): Error: template higher_functions.find(alias pred,T) if (is(typeof(pred(input[0])) == bool)) cannot deduce template function from argument types !(__dgliteral4)(int[])
> higher_functions.d(56): Error: template instance errors instantiating template
> 
> Gotta love those error messages, a C++ programmer will feel right at home! ;)
> 
>  If I add a type specifier like so:
> 
>     auto c = find!(function (int x) { return x < 0; })(a);
>     auto d = find!((int x) { return x < 0; })(a);
> 
> Then it will compile. Should I report this to bugzilla?



More information about the Digitalmars-d mailing list