Order of evaluation for named arguments
Timon Gehr
timon.gehr at gmx.ch
Sat Mar 29 23:00:36 UTC 2025
On 3/29/25 19:38, 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
Fixing the compiler is better. Changing the spec is slightly easier.
More information about the Digitalmars-d
mailing list