Order of evaluation for named arguments
monkyyy
crazymonkyyy at gmail.com
Sat Mar 29 18:45:14 UTC 2025
On Saturday, 29 March 2025 at 18:38:31 UTC, Steven Schveighoffer
wrote:
> In the D spec, the function parameter [section on order of
> evaluation](https://dlang.org/spec/expression.html#order-calls)
> says:
>
>> Arguments are evaluated left to right.
>
> So a question was raised, what about named arguments, since
> those might not match the order of parameters?
>
> I decided to test:
>
> ```d
> import std.stdio;
> void foo(int a, int b) {
> writeln(i"a: $(a), b: $(b)");
> }
>
> void main() {
> int x;
> foo(b: ++x, a: ++x);
> }
> ```
>
> According to the spec, this should print "a: 2, b: 1", as the
> order of evaluation of the expression should go left to right.
> But the actual printout is "a: 1, b: 2".
>
> So the question is, should we modify the spec to reflect this,
> or would it be better to change the compiler to enforce this
> left-to-right evaluation rule?
>
> -Steve
I dont read it as wrong, thats left to right at the callsite;
which if the compiler did extra work to break named arguments
changing the order of operations would be anti-feature
More information about the Digitalmars-d
mailing list