Order of evaluation for named arguments
Salih Dincer
salihdb at hotmail.com
Sun Mar 30 08:59:36 UTC 2025
On Sunday, 30 March 2025 at 07:00:19 UTC, Walter Bright wrote:
>
> I suggest changing the spec.
>
> P.S. the reason D has a defined behavior for this is because
> C/C++ leave it implementation-defined.
C++ does not support named arguments natively, so there’s no
direct comparison. However, function arguments in C++ have
unspecified evaluation order—it is implementation-defined.
In C#, named arguments are evaluated in the order they appear in
the function call, not in the order of parameters.
```C
using System;
class Program {
static void Foo(int a, int b) {
Console.WriteLine($"a: {a}, b: {b}");
}
static void Main() {
int x = 0;
Foo(b: ++x, a: ++x);
}
}
```
> Expected and Actual Output in C#:
>
> a: 2, b: 1
If D wants consistency with other languages like C# and Python,
it should enforce left-to-right evaluation at the call site.
However, modifying the spec would be the "easier" solution, but
that would make D behave unpredictably compared to other
languages.
Given that Walter Bright tends to avoid complex changes, he will
likely suggest updating the spec rather than enforcing
left-to-right evaluation. But if D wants to be intuitive and
predictable, aligning with C#'s approach makes the most sense.
SDB at 79
More information about the Digitalmars-d
mailing list