Pretty please: Named arguments

Don nospam at nospam.com
Tue Mar 1 00:57:20 PST 2011


bearophile wrote:
> Don:
> 
>> You don't have that option. At least, if you're a library developer, you
> don't. (I'm a bit sick of people saying "you don't have to use it if you
> don't want to" in language design. If it is in the language, you don't
> have a choice. You will encounter it).<
> 
> If you add named arguments to D, you are free to not use them when you call Phobos functions or functions from libraries not written by you. 

>You have to see them only if you modify code written by other people that uses named arguments.

This is complete rubbish. You cannot prevent someone from relying on 
your argument names.
Changes to the language *always* affect everyone.

>> There are a couple of things that I really, really don't like about the
>> names argument idea:
>> 1. It makes parameter names part of the API.
>> Providing no way for the function writer to control whether it is part
>> of the API or not,
> 
> Probably I am don't understand something here. Function arguments need to be well chosen,

  so I don't mind them becoming part of the API.

Currently, they are documentation. Readable by a human. Making them part 
of the API is much more restrictive.

Essentially, my chief objection to named arguments is that they severely 
limit your ability to improve the documentation after the function has 
been written.

(In theory an annotation may be used to disallow the usage of named 
arguments for a specific function, but I think this is not necessary).
> 
> 
>> and especially, doing it retrospectively, strikes me
>> as extremely rude.
> 
> There is not much D2 code around, most D2 code is in the future, where this feature will hopefully be known to be present.

No, there's a huge body of existing code, most of it is in C.

>> 2. It introduces a different syntax for calling a function.
>> foo(4, 5);
>> foo(x: 4, y: 5);
>> They look different, but they do exactly the same thing. I don't like
>> that redundancy.
> 
> In SPARK (Ada) they avoid that redundancy because you must use named arguments :-)
> To me they look the same, but the second also tells me what I am assigning numbers to.
> 
> 
>> Especially since, as far as I can tell, the named arguments are just
>> comments (which the compiler can check).
>> If so, a syntax like this would be possible, with no language change at all:
>>
>> pragma(namedarguments); // applies to whole module
>>
>> foo(/*x*/ 4, /*y*/ 5);
>>
>> ---> if a function parameter has a comment which forms a valid
>> identifier, it's a named parameter.
> 
> The two following are the same thing:
> foo(x: 4, y: 5);
> foo(y: 5, x: 4);

Well, if you allowing changes in parameter ordering, it's a very much 
more complicated feature, that frankly I find very scary. It's of 
limited use but enormous complexity.

>> But I still don't see the need for this feature. Aren't people using
>> IDEs where the function signature (with parameter names) pops up when
>> you're entering the function, and when you move the mouse over the
>> function call?
> 
> If you print code on a book or you show code on the web, etc, you don't have an IDE to help you.

You can always put comments in.

> D isn't designed to require an IDE. 

Yes, but this seems like a personal preference.

> And C# devs have added named arguments even if most C# programmers use an IDE. 
> 
> Bye,
> bearophile


More information about the Digitalmars-d mailing list