Pretty please: Named arguments

bearophile bearophileHUGS at lycos.com
Mon Feb 28 05:11:11 PST 2011


Steven Schveighoffer:

> I would say this would be a huge improvement to D.  But I would also say  
>  from my recollection, Walter is very against this.  I think it would take  
> a motivated developer creating a pull request to get it done...

Well implemented named arguments are my #1 enhancement request for D (my #2 request is the tuple unpacking syntax, another thing that's useful all the time).

They are present in some languages as Python. Or in Ada language, where they are named "named arguments":
http://en.wikibooks.org/wiki/Ada_Programming/Subprograms#Named_parameters

In recent C# versions:
http://msdn.microsoft.com/en-us/library/dd264739.aspx
http://geekswithblogs.net/michelotti/archive/2009/01/22/c-4.0-named-parameters-for-better-code-quality.aspx  ) 

If you use named arguments in Python, you learn how much useful they are, I use them often. They don't increase much the complexity of the language for the programmer, they make code less bug-prone, more handy to write, and more self-annotated with compiled-verified annotations.

You have a function like:
void drawRectangle(int height, int width) {...}

You call it with:
drawRectangle(640, 480);

But you may be wrong, having inverted the two values:
drawRectangle(480, 640);

With good named argument you remove that bug source:
drawRectangle(width: 640, height: 480);

Named arguments are so useful that they justify little changes in the compiler too, if necessary.

Bye,
bearophile


More information about the Digitalmars-d mailing list