Hi, I'm trying to make a range.front free function that can be given a defaultValue. Doesn't seem to be working as is written below, seems like the compiler doesn't see the free function as a viable candidate. Isn't it supposed to do its UFCS wizardry and pick up the free func?

import std.stdio;

auto front(Range, T)(Range range, T defaultValue) {
    import std.range: empty, front;
    return range.empty ? defaultValue : range.front;
}

void main()
{
    import std.range: iota;
    0.iota.front(100).writeln;
// Error: inout method std.range.iota!(int, int).iota.Result.front is not callable using a mutable object

    import std.algorithm: filter;
    auto arr = [0, 1];
    arr.filter!"true".front(99).writeln;
// Error: function std.algorithm.iteration.FilterResult!(unaryFun, int[]).FilterResult.front () is not callable using argument types (int)

    arr.front(30).writeln; // OK

    import std.range: front;
    arr.front(30).writeln;
// Error: template std.range.primitives.front cannot deduce function from argument types !()(int[], int), candidates are: std.range.primitives.front(T)(T[] a) ...
}

Cheers

Reply via email to