Passing function parameters by name
Don Clugston
dac at nospam.com.au
Wed Dec 5 00:42:46 PST 2007
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?
Essentially, the name of the argument becomes part of the API. Why isn't
function and object name good enough?
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.
(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?)
More information about the Digitalmars-d
mailing list