libmir / mir-algorithm

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

Sum doesn't work for const(Slice!T*) #454

Closed jmh530 closed 1 year ago

jmh530 commented 1 year ago

mir.math.sum doesn't work with slices where the container itself is const. So for instance, it fails with const(Slice!T*), but not const(Slice!(const(T)*)).

The error it gives here is pretty long, but my testing suggests that the source of the problem is the use of core.lifetime.move. isIterable is true and elementType will Unqual any type that should be getting fed through.

unittest
{
    import mir.math.sum: sum;
    import mir.ndslice.slice: sliced;

    double[] x = [0.0, 1, 2];
    auto y = x.sliced;
    assert(y.sum == 3); //no error

    auto z = y.toConst;
    assert(z.sum == 3); //no error

    const z2 = y;
    //assert(z2.sum == 3); //error for type: const(mir.ndslice.slice.mir_slice!(double*, 1uL, 2).mir_slice)

    const z3 = y.toConst;
    //assert(z3.sum == 3); //error for type: const(mir.ndslice.slice.mir_slice!(const(double)*, 1uL, 2).mir_slice)
}
jmh530 commented 1 year ago

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