Bugs in template constraints

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Aug 3 11:59:22 PDT 2010


There seem to be some bugs with template constraints. Here's a reduce example (from TDPL) which will not compile:

import std.stdio;
import std.range;

@property bool empty(T)(T[] a) { return a.length == 0; }
@property ref T front(T)(T[] a) { return a[0]; }
void popFront(T)(ref T[] a) { a = a[1 .. $]; }


V reduce(alias fun, V, R)(V x, R range)
    if (is(typeof(x = fun(x, range.front)))
        && is(typeof(range.empty) == bool)
        && is(typeof(range.popFront())))
{
    //~ writeln(is(typeof(x = fun(x, range.front))));
    //~ writeln(is(typeof(range.empty) == bool));
    //~ writeln(is(typeof(range.popFront())));
    
    for ( ; !range.empty; range.popFront()) {
        x = fun(x, range.front);
    }
    return x;
}

unittest {
    int[] r = [10, 14, 3, 5, 23];
    
    // compute sum
    int sum = reduce!((a, b) { return a + b; })(0, r);
    assert(sum == 55);
    
    // compute minimum
    int min = reduce!((a, b) { return a < b ? a : b; })(r[0], r);
    assert(min == 3);
}

void main()
{
}

Errors:
reduce_original.d(28): Error: template reduce_original.reduce(alias fun,V,R) if (is(typeof(x = fun(x,range.front))) && is(typeof(range.empty) == bool) && is(typeof(range.popFront()))) does not match any function template declaration
reduce_original.d(28): Error: template reduce_original.reduce(alias fun,V,R) if (is(typeof(x = fun(x,range.front))) && is(typeof(range.empty) == bool) && is(typeof(range.popFront()))) cannot deduce template function from argument types !(__dgliteral1)(int,int[])
reduce_original.d(28): Error: template instance errors instantiating template
reduce_original.d(32): Error: template reduce_original.reduce(alias fun,V,R) if (is(typeof(x = fun(x,range.front))) && is(typeof(range.empty) == bool) && is(typeof(range.popFront()))) does not match any function template declaration
reduce_original.d(32): Error: template reduce_original.reduce(alias fun,V,R) if (is(typeof(x = fun(x,range.front))) && is(typeof(range.empty) == bool) && is(typeof(range.popFront()))) cannot deduce template function from argument types !(__dgliteral4)(int,int[])
reduce_original.d(32): Error: template instance errors instantiating template


If I comment out the constraints, and uncomment those writeln's - which are the same as the constraints - like so:

V reduce(alias fun, V, R)(V x, R range)
    //~ if (is(typeof(x = fun(x, range.front)))
        //~ && is(typeof(range.empty) == bool)
        //~ && is(typeof(range.popFront())))
{
    writeln(is(typeof(x = fun(x, range.front))));
    writeln(is(typeof(range.empty) == bool));
    writeln(is(typeof(range.popFront())));
    
    for ( ; !range.empty; range.popFront()) {
        x = fun(x, range.front);
    }
    return x;
}

I will get all true results back:

true
true
true
true
true
true

I'm filing a bug unless something else is to blame here.


More information about the Digitalmars-d mailing list