Jump to page: 1 24  
Page
Thread overview
Order of evaluation for named arguments
3 days ago
monkyyy
3 days ago
monkyyy
2 days ago
Salih Dincer
3 days ago
Salih Dincer
3 days ago
Salih Dincer
3 days ago
Timon Gehr
3 days ago
Walter Bright
2 days ago
Salih Dincer
2 days ago
Walter Bright
2 days ago
Paul Backus
1 day ago
Timon Gehr
13 hours ago
Walter Bright
8 hours ago
Timon Gehr
13 hours ago
Walter Bright
10 hours ago
claptrap
2 days ago
Daniel N
2 days ago
Salih Dincer
2 days ago
Salih Dincer
22 hours ago
Ogion
22 hours ago
Ogion
1 day ago
Salih Dincer
1 day ago
claptrap
1 day ago
monkyyy
20 hours ago
claptrap
19 hours ago
monkyyy
10 hours ago
claptrap
14 hours ago
Patrick Schluter
10 hours ago
claptrap
10 minutes ago
claptrap
1 day ago
Salih Dincer
3 days ago

In the D spec, the function parameter section on order of evaluation 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:

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

3 days ago

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 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:

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

3 days ago

On Saturday, 29 March 2025 at 18:45:14 UTC, monkyyy wrote:

>

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

No, it's the opposite. It takes the order based on the parameter order (at declaration), not the argument order (at the call site).

I can agree that properly processing the arguments in written order could be more difficult to process in the compiler. But I don't know for sure.

In any case, there is a mismatch between the spec and the implementation.

-Steve

3 days ago

On Saturday, 29 March 2025 at 18:38:31 UTC, Steven Schveighoffer wrote:

>

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?

Given these considerations, I think updating the specification is the more pragmatic approach. The specification could be amended to state:

>

"Positional arguments are evaluated left to right. Named arguments are evaluated in the order of formal parameter declarations."

@SDB@79

3 days ago

Adapting the specification rather than modifying the compiler requires less development effort and leaves the current compiler implementation (e.g., DMD, LDC, GDC) intact.

On Saturday, 29 March 2025 at 18:38:31 UTC, Steven Schveighoffer wrote:

>

or would it be better to change the compiler to enforce this left-to-right evaluation rule?

I changed my mind because qhile positional arguments follow a left-to-right evaluation rule, switching to parameter order for named arguments introduces a duality in the language. This could increase the learning curve and require developers to remember two distinct rules.

🫠

SDB@79

3 days ago
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.

3 days ago

On Saturday, 29 March 2025 at 21:25:17 UTC, Steven Schveighoffer wrote:

>

On Saturday, 29 March 2025 at 18:45:14 UTC, monkyyy wrote:

>

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

No, it's the opposite. It takes the order based on the parameter order (at declaration), not the argument order (at the call site).

I can agree that properly processing the arguments in written order could be more difficult to process in the compiler. But I don't know for sure.

In any case, there is a mismatch between the spec and the implementation.

-Steve

This is a functional vs impertive distinction; math imagines arguments being reduced by algerbra and in that world the call site isnt relivent, but for a machine left to right at the call site is just it doing as told in order.

And Im not saying extra work as in difficult, I mean it functionally the compiler would need to have extra step or do use more storage to make it less expressive then the lazy thing.

3 days ago
On 3/29/2025 4:00 PM, Timon Gehr wrote:
> Fixing the compiler is better. Changing the spec is slightly easier.

I suspect this issue came about because it simply wasn't considered when adding named parameters, and it just fell out this way.

Changing it, though, can silently break existing code which makes people unhappy.

I suggest changing the spec.

P.S. the reason D has a defined behavior for this is because C/C++ leave it implementation-defined.

2 days ago
On Saturday, 29 March 2025 at 23:03:27 UTC, monkyyy wrote:
> This is a functional vs impertive distinction; math imagines arguments being reduced by algerbra and in that world the call site isnt relivent, but for a machine left to right at the call site is just it doing as told in order.
>
> And Im not saying extra work as in difficult, I mean it functionally the compiler would need to have extra step or do use more storage to make it *less expressive* then the lazy thing.

Monkyyy brought up an excellent mathematical point, which adds significant value to the discussion. While Timon provided a summary of the two possibilities, I believe that, given the mathematical foundations of programming, it would be more productive to build upon Monkyyy's perspective rather than approaching it solely from a pragmatic standpoint.

SDB@79

2 days ago

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.

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@79

« First   ‹ Prev
1 2 3 4