Pretty please: Named arguments
bearophile
bearophileHUGS at lycos.com
Mon Feb 28 14:13:04 PST 2011
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.
> 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. (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.
> 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);
While the following ones are not the same call:
foo(/*x*/ 4, /*y*/ 5);
foo(/*y*/ 5, /*x*/ 4);
And if your function is:
void foo(int x = 1, int y = 2) {}
You are also able to write:
foo(y: 5);
That's not equivalent to:
foo(/*y*/ 5);
In Mathematica and Python there are plotting functions that have lot of parameters, all of them have a default argument. With named arguments you are able to choose to modify only one or few arguments, keeping your code readable and short and bug-free.
> 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.
D isn't designed to require an IDE. 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