Named Arguments Status Update - Overloading by name

jmh530 john.michael.hall at gmail.com
Wed Apr 10 12:18:06 UTC 2024


On Wednesday, 10 April 2024 at 12:00:42 UTC, jmh530 wrote:
> On Tuesday, 9 April 2024 at 21:33:21 UTC, Lance Bachmeier wrote:
>> On Tuesday, 9 April 2024 at 21:15:59 UTC, Uranuz wrote:
>>
>>> I was thinking that assignment could be the reason, because 
>>> assigment could have some return value.
>>
>> Not just that, but also the way you can write function calls 
>> as assignments, even when you wouldn't expect it. A very nice 
>> feature when you have a use for it, but it also lets rebels 
>> write stuff like this:
>>
>> ```
>> import std;
>>
>> void main() {
>>     writeln(f=4.7);
>>     auto h=g(f=4.7);
>>     writeln(h);
>> }
>>
>> double f(double x) {
>>     return 2.5*x;
>> }
>>
>> double g(double x) {
>>     return 2.5*x;
>> }
>> ```
>
> That's a new one for me! Not sure I would want to make a habit 
> of that though!

That line with `auto h` can also be written `auto h = g = f = 
4.7;` and you get the same result.

Learn something new every day, I guess. The function spec [1] 
doesn't talk about this, but it must be something with an assign 
expression [2] of f = a getting re-written to f(a) for functions.

[1] https://dlang.org/spec/function.html
[2] https://dlang.org/spec/expression.html#assign_expressions


More information about the Digitalmars-d mailing list