On 2017-10-05 00:59, Walter Bright wrote:

An example would be appreciated. Timon's example requires guesswork as to what he intended, because it does not compile in ways unrelated to his point.

It's supposed to not compile, because D doesn't have ADL.

$ cat foo.d
module foo;
import std.range: isInputRange;

auto sum(R)(R r)if(isInputRange!R){
    typeof(r.front) result;
    for(auto t=r.save;!t.empty;t.popFront())
        result+=t.front;
    return result;
}

$ cat main.d
import foo;
import std.range;

void main(){
    int[] a = [1,2,3,4];
    import std.stdio: writeln;
    writeln(a.front); // ok
    writeln(sum(a)); // error, the type is an input range, yet has no front
}

$ dmd foo.d main.d
foo.d(5): Error: no property 'front' for type 'int[]'
foo.d(6): Error: no property 'save' for type 'int[]'
main.d(8): Error: template instance foo.sum!(int[]) error instantiating

Adding the following line to the "foo" module fixes the problem:

import std.range : front, save, empty, popFront;

 $ cat foo.d
module foo;
import std.range: isInputRange;
import std.range : front, save, empty, popFront;
auto sum(R)(R r)if(isInputRange!R){
    typeof(r.front) result;
    for(auto t=r.save;!t.empty;t.popFront())
        result+=t.front;
    return result;
}

$ dmd foo.d -run main.d
1
10

--
/Jacob Carlborg

Reply via email to