Pretty please: Named arguments

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 28 14:13:21 PST 2011


On Mon, 28 Feb 2011 16:38:34 -0500, Don <nospam at nospam.com> wrote:

> spir wrote:
>>  Just don't use them!
>
> 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).

encounter *reading* it or encounter *using it*?  You shouldn't ever have  
to use named parameters if you don't want to.  Just like you can specify  
all defaulted parameters, or specify all template args when calling  
templated functions.

I could live without named parameters (obviously!), but I certainly think  
reading a call with named parameters can be much easier with the parameter  
names than without, and I don't think you could ever say the opposite  
(that name-less parameters would be clearer).

> 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, and especially, doing it retrospectively, strikes me  
> as extremely rude.
>
> 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.

I look at it similarly to IFTI:

foo(T)(T t) {...}

foo(1);
foo!int(1);

both mean the same, the int is optional.

Note that the library developer of foo has no way to restrict you from  
using either form.  One is simply a clearer way of saying the other.

In essense, the parameter names are ALREADY an essential part of the API.   
If we didn't name the parameters (entirely possible with .di files!), how  
shitty would programming be?

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

There is a feature that isn't available via comments, parameter reordering:

foo(y: 5, x: 4);

Who in their right mind wants to do this, right?  Well, consider you use  
two different libraries, and in one library the y is always the first  
parameter, and in another the x is always the first parameter (yes, I've  
seen shit like this).  Calls to the separate libs might be a nightmare of  
continuous documentation referencing.

But if you can just always say "x is first", then the calls are easy to  
read and write, and completely unambiguous.

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

Dunno, vim doesn't do that for me currently.  Also, if reviewing code on  
github, there is no ide.

-Steve


More information about the Digitalmars-d mailing list