libmir / mir-algorithm

Dlang Core Library
http://mir-algorithm.libmir.org
Other
173 stars 37 forks source link

filter and each compiler error #448

Closed fabienmica closed 1 year ago

fabienmica commented 1 year ago

I have got a compile error when using filter together with each

import mir.algorithm.iteration: each, filter;
import mir.ndslice.topology : map;

//Does not compile
 [1, 2, 3].filter!(x=>x>=2)
          .each!(x=>writeln(x));

 //Compile
 [1, 2, 3].map!(x=>2*x)
          .each!(x=>writeln(x));

Error: none of the overloads of templatemir.primitives.anyEmptyare callable using argument types!()(Filter!(__lambda1, Slice!(int*, 1LU, mir_slice_kind.contiguous)))``

jmh530 commented 1 year ago

Below is anyEmpty from mir.primitives. The body of the function allows for the Range to have anyEmpty or shape and then falls back to empty. However, the template constraint does not allow for that. Maybe an additional check that the range can call empty would resolve this issue.

bool anyEmpty(Range)(scope const auto ref Range range) @property
    if (hasShape!Range || __traits(hasMember, Range, "anyEmpty"))
{
    static if (__traits(hasMember, Range, "anyEmpty"))
    {
        return range.anyEmpty;
    }
    else
    static if (__traits(hasMember, Range, "shape"))
    {
        return anyEmptyShape(range.shape);
    }
    else
    {
        return range.empty;
    }
}
jmh530 commented 1 year ago

Fixed after https://github.com/libmir/mir-algorithm/pull/456