On 13.01.2018 01:20, Mark wrote:

Could we also support function tuples?

In principle, yes, though I imagine it is a lot harder to argue for its inclusion than for that of the features currently proposed in the DIP, because existing language features already come rather close.

For instance:

int sum(int a, int b) { return a+b; }
int diff(int a, int b) { return a-b; }

(int, int) f = (sum, diff) // no ";" for consistency with existing syntax

No ";" is inconsistent. Also the type of f cannot be (int, int), and (sum, diff) would try to call sum and diff with 0 arguments and fail.

int (x,y) = f(1, 2); // x=3, y=-1
int (w,z) = (1, 2).f() // same as above, UFCS
int (u,v) = (1, 2).(sum, diff) // same as above, "lambda tuple"

In the last example, (sum, diff) is basically lowered to (x,y) => (sum(x,y), diff(x,y)).

I think this is easy enough to achieve without dedicated syntax:

import std.typecons;

template apply(F...){
    auto apply(T)(T arg){
        import std.conv, std.range, std.algorithm;
return mixin(text("(",iota(F.length).map!(i=>text("F[",i,"](arg)")).join(","),")"));
    }
}

void main(){
    int sum(int a, int b) { return a+b; }
    int diff(int a, int b) { return a-b; }
    auto (u,v)=(1,2).apply!(sum, diff);
    import std.stdio;
    writeln(u," ",v);
}

Reply via email to