Pretty please: Named arguments

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue Mar 1 01:51:07 PST 2011


On Mon, 28 Feb 2011 13:50:52 -0800, Jonathan M Davis wrote:

> On Monday, February 28, 2011 13:38:34 Don wrote:
>> spir wrote:
>> > On 02/28/2011 07:51 PM, Jonathan M Davis 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.
>> 
>> I agree.
>> CreateFont() in the Windows API, I'm looking at you. (For Linux people,
>> that function has about 12 parameters).
>> 
>> > ???
>> > 
>> >> 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,  [...]
>> > 
>> > 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).
>> 
>> 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.
>> 
>> 
>> 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.
>> 
>> 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?
>> And if you really want to see them all the time, why not build that
>> feature into the IDE?
>> ("hit ctrl-f9 to show all parameter names, hit it again to hide them").
> 
> I agree with pretty much everything said here. However, as I understand
> it, named parameters (at least as they work in Python) would allow for
> you to reorder parameters and give values for paramters which are
> normally default parameters without giving values for the default
> paramters before them, and those changes could not be dealt with by
> comments. [...]

I think I agree with you and Don here.  As for "skipping" default 
parameters, I suggest the following syntax:

  void foo(int i, bool b = true, real r = 3.14, string s = "")
  { ... }

  foo(1, , , "Hello World!");

This is a much smaller language change than named parameters.

-Lars


More information about the Digitalmars-d mailing list