[comp.std.c++] named parameters

schemers@vela.acs.oakland.edu (Roland Schemers III) (02/13/91)

I was wondering if the concept of named parameters has ever came up.
Right now you can declare a function like so:

some_func(int a, int b=0);

But since you can only have default values listed at the end this makes for
a real pain. Has anyone proposed the following:

some_func(int a=10, int b=0);

Then being able to say something like:

main()
{
     some_func(b:=10);
}

Thus by naming the parameter 'b' by using the ':=', you could then specify
the parameters in any order. VMS Pascal lets you do this and I think Ada
also has this. It would make calling functions like:

wind = new Window(width:=80,height:=24,x:=0,y:=0);

Much easier. It would also let you declare a function like:

some_func(int a=10, int b, int c=10);

and call it like:

some_func(b:=5);

Of course this brings up some problems with:

some_func(int a=10, int b);
some_func(int b);

But that could be flagged as ambigious.

It seems like the ':=' would be a good token to use for this syntax, although
people might get it confused with '='. There is also => which could also be 
used.

Comments?

Roland

-- 
Roland J. Schemers III                              Systems/Network Manager
schemers@vela.acs.oakland.edu (Ultrix)              Oakland University 
schemers@argo.acs.oakland.edu (VMS)                 Rochester, MI 48309-4401
You can tune a piano, but you can't tuna VMS!       (313)-370-4323

garry@ithaca.uucp (Garry Wiegand) (02/16/91)

In article <5120@vela.acs.oakland.edu> schemers@vela.acs.oakland.edu (Roland Schemers III) writes:
>I was wondering if the concept of named parameters has ever came up.
>Right now you can declare a function like so:
>
>some_func(int a, int b=0);
>
>But since you can only have default values listed at the end this makes for
>a real pain. Has anyone proposed the following:
>
>some_func(int a=10, int b=0);
>...
>Comments?

Roland -

It would be lovely. When I read through the ARM I red-flagged the

    some_func(int a, int b=0)

syntax as a stylistic Bad Idea - it's OK for one (or maybe two)
optional arguments, but for multiple optional arguments it
degenerates into something that is very hard to explain to a "normal
human". 

Ie, if you have a function with parameters (a,b,c,d)

it's purely good luck if the argument "d" that would logically be
mentioned last also happens to be the argument that would naturally
be omitted first, 

it's unusual that every single time you might want to omit "c" you
will already have decided to omit "d", 

it's improbable that every single time you might want to omit "b" you
will already have decided to omit "c" and "d", and

it's approaching absurd (except in specially-constructed cases) that
every single time you might want to omit "a" you will already have
decided to omit "b", "c" and "d".

So it looks to me like the ARM's optional-argument style is
brain-dead, and just clutters up the language to no good purpose.

Real optional arguments, such as you propose, would be very
interesting, but you will probably run into the argument that the
":=" pseudo-operator "doesn't look like C". 

Garry Wiegand    ---    Ithaca Software, Alameda, California
...!uunet!ithaca!garry, garry%ithaca.uucp@uunet.uu.net