The following program
---8<--- class foo { string `+(mixed ... args) { return "left:"+sizeof(args); } string ``+(mixed ... args) { return "right:"+sizeof(args); } }
int main() { foo x = foo(); write("%s\n%s\n%s\n", x+x+x+x+x, (((x+x)+x)+x)+x, x+(x+(x+(x+x)))); return 0; } ---8<---
gives the output
left:4 left:4 left:1
(tested in Pike 7.8.350). Shouldn't the last line be "right:4"? We seem to be missing an opportunity for optimization here.
(tested in Pike 7.8.350). Shouldn't the last line be "right:4"? We seem to be missing an opportunity for optimization here.
The `+ operator prefers `+() to ``+() when both are available. Note though that even if ``+() had been called the result would've been "right:1", since the arguments to ``+() are supposed to be evaluated left-recursively (ie a typical implementation may call predef::`+() on the arguments to contract them).
So when _would_ ``+ get more than one argument then? I don't get where this supposition comes from.
I see the code, but not the reasons. If the terms are left-recursive, why not just collapse them _before_ calling ``+? It makes more sense to suppose that the arguments to ``+ are evaluated right-recursively, IMO.
I see the code, but not the reasons. If the terms are left-recursive, why not just collapse them _before_ calling ``+? It makes more sense
In practice, this seems to happen; I haven't been able to make a test case where lfun::``+() gets more than one argument.
to suppose that the arguments to ``+ are evaluated right-recursively, IMO.
In practice, this seems to happen; I haven't been able to make a test case where lfun::``+() gets more than one argument.
I have: `+(1,2,3,x,4,5,6) calls x->``+(1,2,3). (Pike 7.8.350.) However, I don't see any reason for this. It should call x->``+(6), just like 1+2+3+x+4+5+6 does.
So I still believe that ``+ with multiple arguments should be used for right-recursive sums.
pike-devel@lists.lysator.liu.se