What to do about default function arguments
Jonathan M Davis
jmdavisProg at gmx.com
Thu Apr 26 10:25:35 PDT 2012
On Thursday, April 26, 2012 15:09:15 Joseph Rushton Wakeling wrote:
> On 26/04/12 05:44, Walter Bright wrote:
> > But if we make default arguments solely a part of the function
> > declaration, then function pointers (and delegates) cannot have default
> > arguments. (And maybe this isn't a bad thing?)
>
> I can't see disallowing default arguments as being a good thing. For
> example, instead of,
>
> void foo(int a, int b = 2)
> {
> ...
> }
>
> surely I can just put instead
>
> void foo(int a, int b)
> {
> ...
> }
>
> void foo(int a)
> {
> foo(a, 2);
> }
>
> ... and surely I can do something similar for function pointers and
> delegates. So, I can still have default arguments in effect, I just have to
> work more as a programmer, using a less friendly and easy-to-understand
> syntax. That doesn't really seem like a good way to operate unless there's
> an extreme level of complication in getting the compiler to handle the
> situation.
There is an _enormous_ difference between disallowing default arguments in
general and disallowing them in function pointers and delegates.
Think about how function pointers and delegates get used. They're almost
always called generically. Something gets passed a function pointer or
delegate and argument and calls it. The caller isn't going to care about
default arguments. It expects a specific signature and passes those specific
arguments to the function/delegate when it calls it. It's not going to be
calling it with 2 arguments in one case and 3 in another.
No default arguments are involved with function pointers in C/C++, and I've
never heard of anyone complain about it there. Default arguments are great
when you're going to be calling a function in a variety of places, and you
want defaults for some parameters in cases where they're almost always a
particular value so that you don't have to always type them. But function
pointers and delegates are generally called in _one_ way, if not in one place,
so their usage is _very_ different.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list