www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Wish item: Function arguments by name

reply Eric Shumard <Eric_member pathlink.com> writes:
A wish item for D: function arguments specified by name instead of position.
a function would still be specified as usual:

long sum(long start, long end, long increment) {
long result = 0;
long index;
for (index=start; index<=end; index+=increment) {
result += index;
}
return result;
}

Currently, the function is called with parameters specified by position:

result = sum(3, 8, 2);

Calling by name would look like:

result = sum(start=3, end=8, increment=2);

This is much more readable which is its biggest virtue.

You could also use:

result = sum(increment=2, end=8, start=3);

That is, you can present the arguments in any order.
It gives the ability to have default values for any subset of arguments (not
just the trailing arguments).

The function could be specified with default values as

long sum(long start=1, long end=2, long increment=1);

Then invoked:

result = sum(start=1, end=8);
result = sum(end=10);
etc...

You can still invoke a function the old way.

This feature exists in VHDL, a hardware description language that I use
extensively.
May 07 2004
next sibling parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
I would like that also - it would help with generic programming - but only if it
doesn't screw up the rest of the language.

"Eric Shumard" <Eric_member pathlink.com> wrote in message
news:c7hkj1$2jbg$1 digitaldaemon.com...
 A wish item for D: function arguments specified by name instead of position.
 a function would still be specified as usual:

 long sum(long start, long end, long increment) {
 long result = 0;
 long index;
 for (index=start; index<=end; index+=increment) {
 result += index;
 }
 return result;
 }

 Currently, the function is called with parameters specified by position:

 result = sum(3, 8, 2);

 Calling by name would look like:

 result = sum(start=3, end=8, increment=2);

 This is much more readable which is its biggest virtue.

 You could also use:

 result = sum(increment=2, end=8, start=3);

 That is, you can present the arguments in any order.
 It gives the ability to have default values for any subset of arguments (not
 just the trailing arguments).

 The function could be specified with default values as

 long sum(long start=1, long end=2, long increment=1);

 Then invoked:

 result = sum(start=1, end=8);
 result = sum(end=10);
 etc...

 You can still invoke a function the old way.

 This feature exists in VHDL, a hardware description language that I use
 extensively.

May 07 2004
prev sibling next sibling parent Mark T <Mark_member pathlink.com> writes:
In article <c7hkj1$2jbg$1 digitaldaemon.com>, Eric Shumard says...
A wish item for D: function arguments specified by name instead of position.
a function would still be specified as usual:

long sum(long start, long end, long increment) {

this originated in Ada (which inspired VHDL) Ada BNF: "parameter_association ::= [formal_parameter =>] actual_parameter" example Ada procedure call: PRINT_HEADER(HEADER => TITLE, CENTER => TRUE, PAGES => 128);
Currently, the function is called with parameters specified by position:

result = sum(3, 8, 2);

Calling by name would look like:

result = sum(start=3, end=8, increment=2);

I have seen people do this in other languages with comments: result = sum( 3, /* <= start */ 8, /* <= end */ 2 /* <= increment */ ); but that gets a little unwieldy with proper naming of variables (how often does one actually call functions with numeric literals) and context sensitive function look-up in IDEs, I would say that it probably just adds clutter to the code.
May 08 2004
prev sibling parent Juanjo =?ISO-8859-15?Q?=C1lvarez?= <juanjuxNO SPAMyahoo.es> writes:
I would _love_ to have this feature, I use it a lot at work in Python just
because it helps readability (and I work in a team). Of course this is more
usefull in Python since the language is dinamically typed and as such other
people reading your code sometimes can have headaches trying to guess the
arguments to be passed to some function/method.

Eric Shumard wrote:

 A wish item for D: function arguments specified by name instead of
 position. a function would still be specified as usual:

May 19 2004