What to do about default function arguments
Steven Schveighoffer
schveiguy at yahoo.com
Thu Apr 26 06:08:07 PDT 2012
On Wed, 25 Apr 2012 23:44:07 -0400, Walter Bright
<newshound2 at digitalmars.com> wrote:
> 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?)
Some testing (2.059):
void main()
{
auto a = (int x = 1) { return x;};
auto b = (int x) { return x;};
pragma(msg, typeof(a).stringof);
pragma(msg, typeof(b).stringof);
}
output:
int function(int x = 1) pure nothrow @safe
int function(int x = 1) pure nothrow @safe
second pass:
void main()
{
auto a = (int x = 1) { return x;};
pure nothrow @safe int function(int) b = (int x) { return x;};
pragma(msg, typeof(a).stringof);
pragma(msg, typeof(b).stringof);
b = a; // ok
//a = b; // error
//b(); // error
}
output:
int function(int x = 1) pure nothrow @safe
int function(int)
if you ask me, everything looks exactly as I'd expect, except the auto
type inference of b. Can this not be fixed? I don't understand the
difficulty.
BTW, I didn't know you could have default arguments for
functions/delegates, it's pretty neat :)
-Steve
More information about the Digitalmars-d
mailing list