Pretty please: Named arguments

spir denis.spir at gmail.com
Mon Feb 28 12:18:10 PST 2011


On 02/28/2011 08:33 PM, Jonathan M Davis wrote:
> On Monday, February 28, 2011 11:02:37 Steven Schveighoffer wrote:
>> On Mon, 28 Feb 2011 13:51:56 -0500, Jonathan M Davis<jmdavisProg at gmx.com>
>>
>> wrote:
>>> I'm not entirely against named arguments being in D, however I do think
>>> that any
>>> functions that actually need them should be refactored anyway. So,
>>> ultimately,
>>> I'm not sure that they're really all that useful. I'm sure that they'd
>>> be useful
>>> upon occasion, but if you actually need them, then your function is
>>> taking too
>>> many arguments.
>>>
>>> In actuality, if I were to vote on whether named arguments should be in
>>> the
>>> language, I would definitely vote against it (I just plain don't want
>>> the code
>>> clutter, and they strike me as a crutch to avoid writing functions with
>>> good
>>> signatures in spite of their usefulness in some situations), but I can
>>> see why
>>> some people might want them.
>>
>> Although I am not strongly for named arguments, I think they would be a
>> definite improvement.
>>
>> Bearophile brought up one of the strongest cases for them:
>>
>> foo(int width, int height) {}
>>
>> Seems simple enough, I don't see how you have "too many arguments", but
>> the call looks like this:
>>
>> foo(123, 456);
>>
>> So, looking at this call, can you tell which is width and which is
>> height?  I've seen some libs that use width and height do height first
>> also.  I usually have to go look up the API every time I'm reading/writing
>> one of these.
>>
>> But this is perfectly clear and resists API changes/differences:
>>
>> foo(width: 123, height: 456);
>>
>> The cool part about this is, named arguments are not required -- you can
>> always just not use them.  But when you do use them, the code becomes much
>> clearer.
>
> That does have some minimal benefit, but if you're really passing around width
> and height much, then I'd argue that they should be put in a struct rather than
> passed around bare like that, and then that fixes the issue.

Precisely! Named arguments are a way to make an actual-parameter set a kind of 
named tuple, meaning a "free struct".
Another place where it would be nice is return values:
     XYZ dimension (...) {
         ...
         return (width:123, height:456);
     }

<OT>
By the way, I wonder why people tend to systematically add one, and only one, 
space after ':', ',', ';' and never before. D code is not english! There is no 
reason to write "width: 123" rather than "width:123" or "width : 123". Rather 
the opposite, ':' expresses here a link which does not binds tightlier on left 
side than on right side ;-)
Same about ';': it's a separator that has no reason to stick on left side, and 
not on right side!
People are so conservative...
</OT>

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d mailing list