What to do about default function arguments

Michel Fortin michel.fortin at michelf.com
Thu Apr 26 04:01:37 PDT 2012


On 2012-04-26 03:44:07 +0000, Walter Bright <newshound2 at digitalmars.com> said:

> A subtle but nasty problem - are default arguments part of the type, or 
> part of the declaration?
> 
>     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.
> 
> 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?)

Assuming we want to allow it, all you need is to treat the type having 
the default parameters as a specialization of the type that has none. 
In other words, the type with the default arugments is implicitly 
castable to the type without.

Should it affect name mangling? Yes and not. It should affect name 
mangling in the compiler since you're using the mangled name to check 
for equality between types. But I think it should not affect name 
mangling for the generated code: the base type without the default 
arguments should give its name to the emitted symbols so that changing 
the default argument does not change the ABI.

But is it desirable? I'm not convinced. I don't really see the point. 
It looks like a poor substitute for overloading to me.

That said, there was some talk about adding support for named 
parameters a year ago. For named parameters, I think it'd make sense to 
have parameter names be part of the type, and little harm would result 
in adding default parameters too into the mix. As suggested above, it 
should be implicitly castable to a base type without parameter names or 
default values. But for default parameters alone, I don't feel the 
complication is justified.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list