Passing function parameters by name

renoX renosky at free.fr
Thu Dec 6 13:50:29 PST 2007


Janice Caron a écrit :
> On Dec 5, 2007 1:48 PM, renoX <renosky at free.fr> wrote:
>> 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?
> 
> I often write constructor functions which look something like this
> 
>     this(int x_, int y_, int z_)
>     {
>         x = x_;
>         y = y_;
>         z = z_;
>     }
> 
> If parameter names were part of the API, then anyone calling my code
> would have to refer to those constructor parameters as x_, y_ and z_,
> complete with trailing underscores

Would have?
No only could: nobody said that passing parameter by name should be 
mandatory.

As for the rest, yes having the possibility to pass parameter by name 
would mean that programmers will try to find better name for the 
parameters, but that's a plus if you believe in literate programming..

> Of course, I could go back and change all my code to
>     this(int x, int y, int z)
>     {
>         this.x = x;
>         this.y = y;
>         this.z = z;
>     }

I'm not sure that the trailing underscores would be that hurtful to the 
users..
Maybe such feature could be advertised 6 month before: "In 6 month, the 
name of your parameter will become visible to the users, please ensure 
that your parameters names are well-chosen."

> but that would be a lot of code to change, and it might break calling code!

Somehow Python, Ada manage to have this feature without issue, I've seen 
plenty of rants against both language but *never* read complaints 
against Python or Ada "passing parameter by name" feature..

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