www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TDPL: Function literals with missing argument type specifiers do not compile

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Aug 02 2010
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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?
Aug 02 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Update: This seems to have been fixed in 2.048. But I don't see it mentioned in
the changelog, so I'm not sure if it was fixed for good or fixed by accident. :)

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?
Aug 21 2010