Passing function parameters by name

renoX renosky at free.fr
Wed Dec 5 05:48:37 PST 2007


Don Clugston Wrote:
> Artyom Shalkhakov wrote:
> > Hello everyone.
> > 
> > I suggest adding a new syntax for function parameter passing. To illustrate:
> > 
> > void foo( int a, float b ) {
> >     // some code
> > }
> > 
> > void main() {
> >     foo( 1, 2.5f ); // classic
> >     foo( b : 2.5f, a : -3 ); // sort of struct initializer
> > }
> > 
> > Since named arguments can be passed in any order, they can be especially useful when a function has more than one parameter with a default value.
> > 
> > This concept could be applied to template instantiation as well.
> > 
> > -Artyom Shalkhakov
> 
> (Genuine question, not a flame):
> To play devil's advocate for a moment: doesn't a long parameter list or lots of 
> default arguments indicate a poor design?


A long list of parameter is not needed in order to have parameter passing par name useful: some compilers have added specific checks for memset (only 3 arguments) because quite a few people made an error in the order of parameters..


> Essentially, the name of the argument becomes part of the API. Why isn't 
> function and object name good enough?


I could ask the opposite question: why should we restrict the API to the function and object name instead of using the full data that we have?
To answer your question: passing parameter by name increase the readability of the source, reduce the number of mistake when writing the code (and with a good IDE the number of character to type would be the same).


> Named arguments are potentially a disastrous feature, if completely 
> unrestricted. It was when COM needed to support VB's named arguments that 
> Windows programming really nose-dived.

Could you explain this point? (I know nothing about COM).

> (OTOH: A string mixin can, given the name of a function, tell you what the names 
> of all of it's default arguments are (as well as what their default values are).
> I can in fact write a string mixin implementation of this feature; it's 
> perfectly feasible. But is the concept actually a good idea?)

Note that passing parameter by name is only useful if programmers doesn't have to jump through hoops to do it, otherwise nobody will use it.

renoX



More information about the Digitalmars-d mailing list