What to do about default function arguments
Jonathan M Davis
jmdavisProg at gmx.com
Thu Apr 26 10:57:14 PDT 2012
On Thursday, April 26, 2012 19:45:55 Joseph Rushton Wakeling wrote:
> On 26/04/12 19:25, Jonathan M Davis wrote:
> > There is an _enormous_ difference between disallowing default arguments in
> > general and disallowing them in function pointers and delegates.
>
> I think maybe I've misunderstood the problem. Are we talking about default
> values _of the function pointer_ or that get passed to the function pointer?
>
> i.e. are we talking about,
>
> int foo(int delegate() dg = &bar) {
> ...
> }
>
> or about
>
> int foo(int delegate() dg = &bar) {
> bar(); // assumes default arguments for bar
> }
The second. If you have
int foo(int a = 1)
{
return a + 3;
}
and you call foo directly without any arguments, then the default argument
would be inserted. The problem is when you have a pointer to foo. Should calls
to the pointer use a default argument or not? To do that, they need to be part
of the type of the function pointer (or delegate).
Personally, I think that it's a very clear and resounding _no_. Default
arguments are merely syntactic sugar (albeit very useful syntactic sugar) and
should _not_ affect the type of a function pointer or delegate. A function
pointer shouldn't care about whatever default arguments its function might
have, and a pointer to the foo above should have the same type as a pointer to
int bar(int a)
{
return a - 2;
}
As such, code like
auto foo = (int a = 1) { return a;};
should probably be illegal, since the default argument could never be used.
Right now, we get very weird behavior due to an attempt to make such things
work. And I think that it's pretty clear that that was a mistake (though
obviously this discussion is to gauge what the group as a whole think and
debate it if necessary).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list