What to do about default function arguments

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Apr 25 21:42:26 PDT 2012


On Wed, Apr 25, 2012 at 08:44:07PM -0700, Walter Bright wrote:
> A subtle but nasty problem - are default arguments part of the type,
> or part of the declaration?

My intuition suggests they are part of the declaration, but not part of
the type. For example, you could have two functions:

	void f(int a, int b);
	void g(int a, int b=1);

Are they the same type? I say yes, because both functions accept two int
arguments. Nothing about g suggests that it's any different from f,
except for the syntactic sugar that you can write g(1) instead of
g(1,1). So the default argument really is just part of the declaration,
not the type.


>    See http://d.puremagic.com/issues/show_bug.cgi?id=3866
> 
> Currently, they are both, which leads to the nasty behavior in the bug
> report.
> 
> The problem centers around name mangling. If two types mangle the
> same, then they are the same type. But default arguments are not part
> of the mangled string. Hence the schizophrenic behavior.

This is bad. What if you have two delegates with different default
arguments? What would this code do:

	auto foo = (int a=1) { return a; };
	auto bar = (int a=2) { return a; };
	writeln(foo());
	writeln(bar());

?


> 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 see default arguments as syntactic sugar; they are to avoid typing
commonly used trailing arguments repeatedly. They don't make sense in
the context of func ptrs and delegates because presumably you're using a
func ptr or a delegate for generic handling, as Jonathan said, so you
wouldn't be relying on default arguments anyway. So default arguments
aren't really useful in this case. No big loss if we lose them.


T

-- 
Computers aren't intelligent; they only think they are.


More information about the Digitalmars-d mailing list