stdio line-streaming revisited
Walter Bright
newshound1 at digitalmars.com
Thu Mar 29 22:40:34 PDT 2007
kris wrote:
> Cout.opCall("Hello, ").opCall(Cin.get);
Given:
a.f(b,c);
can be equivalently written as:
f(a,b,c);
we can transform:
Cout.opCall("Hello, ").opCall(Cin.get);
into:
opCall(opCall(Cout,"Hello, "), Cin.get);
And it is transformed that way, because the optimizer/back end knows
nothing about member functions. They're just more function calls.
The nested opCall will get called before the outer opCall, but otherwise
the order is undefined (and will change depending on the function
calling convention, whether it is a virtual call or not, etc.).
I suggest that at a minimum, if you wish to retain the current design,
build a test into the test suite that verifies the required order of
evaluation.
It's possible that in the future, to ensure source code portability of
D, that the order of evaluation will become fixed. Such a change is a
fair amount of work to accomplish, and will incur a runtime penalty (the
implementation defined order allows the compiler to rearrange things to
minimize register pressure). Even if the order was fixed, it still might
not be in the right order for call chaining to work as your design needs
it to.
I also suggest that, with the maturing of the variadic template
capability of D, using it will side step the order of evaluation problem
completely. It'll still be an aesthetically pleasing design to the user.
More information about the Digitalmars-d
mailing list