phobos
phobos copied to clipboard
`std.algorithm : fold` has an over-eager `static assert`
import std;
void main () {
auto pad = (string[]).init.map!(c => c.length).fold!max(0);
}
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(4456): Error: static assert: "Incompatible function/seed/element: std.algorithm.comparison.max/int/ulong"
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(4440): instantiated from here: `reduceImpl!(false, MapResult!(__lambda2, string[]), int)`
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(4424): instantiated from here: `reducePreImpl!(MapResult!(__lambda2, string[]), int)`
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(4863): instantiated from here: `reduce!(int, MapResult!(__lambda2, string[]))`
onlineapp.d(3): instantiated from here: `fold!(MapResult!(__lambda2, string[]), int)`
Oh and whoever decided this was a good idea... Was wrong:
import std;
void main () {
auto pad = (string[]).init.map!(c => c.length).fold!max; // No explicit seed
}
object.Exception@/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(4389): Cannot reduce an empty input range w/o an explicit seed value.
----------------
/dlang/dmd/linux/bin64/../../src/phobos/std/exception.d:521 pure @safe noreturn std.exception.bailOut!(Exception).bailOut(immutable(char)[], ulong, scope const(char)[]) [0x560548357c37]
/dlang/dmd/linux/bin64/../../src/phobos/std/exception.d:442 pure @safe bool std.exception.enforce!().enforce!(bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong) [0x560548357bb6]
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d:4389 pure @safe ulong std.algorithm.iteration.reduce!(std.algorithm.comparison.max).reduce!(std.algorithm.iteration.MapResult!(onlineapp.main().__lambda2, immutable(char)[][]).MapResult).reduce(std.algorithm.iteration.MapResult!(onlineapp.main().__lambda2, immutable(char)[][]).MapResult) [0x560548358998]
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d:4863 pure @safe ulong std.algorithm.iteration.fold!(std.algorithm.comparison.max).fold!(std.algorithm.iteration.MapResult!(onlineapp.main().__lambda2, immutable(char)[][]).MapResult).fold(std.algorithm.iteration.MapResult!(onlineapp.main().__lambda2, immutable(char)[][]).MapResult) [0x56054835894e]
./onlineapp.d:5 _Dmain [0x560548357a8b]
Not overeager, it's based on the implementation. The code uses the parameter as storage for the result. Instead, you have to declare a tuple of typeof(fn[0](seed[0], e)), typeof(fn[1](seed[1], e)), ... to hold the results.
With single seed/function the code would be straightforward, but with multiple seeds/functions, it's not straightforward.
Doable, but maybe rearranging how the code works could make it easier.