On Tuesday, 4 April 2017 at 05:04:04 UTC, Dukc wrote:
On Monday, 3 April 2017 at 21:43:41 UTC, Meta wrote:
On Monday, 3 April 2017 at 21:33:20 UTC, Timon Gehr wrote:
Any suggestions as to how to get something similar working?

auto fold(string op, Args...)(Args args)
{
    foreach(e; args[1 .. $]) args[0] += e;
    return args[0];
}

void main()
{
    import std.stdio;
    fold!"+"(1, 2, 3).writeln; //6
}

This does work I guess. I'm not sure if the foreach will be unrolled or not but if it is I guess this is fairly close to the C++ example. However, it's still more verbose. My goal was to emulate almost exactly what C++ was doing by using a template so you could just write "fold!('+', args)" and have it automatically rewritten as "Args[0] + Args[1] + Args[2] ...". However it gets difficult because the compiler is trying to interpret args at compile time.

Reply via email to