April 26, 2012
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?)
April 26, 2012
On Thursday, 26 April 2012 at 03:44:27 UTC, Walter Bright 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?)

I don't think it's a bad thing: default arguments in the type system mean values in the type system.

NMS
April 26, 2012
On Wednesday, April 25, 2012 20:44:07 Walter Bright 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?)

Can function pointers have default arguments in C? Honestly, it strikes me as rather bizarre for them to have default arguments. I really don't think that they buy you much.

If you use the function or delegate immediately after declaring it (as is typically the case when they're nested), then you could have just just put the default argument in the function itself and not have it as a parameter. And if you're holding on to a function or delegate long term, you're almost certainly going to be using it generically, in which case I wouldn't expect a default argument to make sense there often either.

I'd vote to just disallow default arguments for function pointers and delegates.

- Jonathan M Davis
April 26, 2012
On 4/25/2012 8:44 PM, Walter Bright wrote:
> 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.

One might suggest mangling the default argument into the type. But default arguments need not be compile time constants - they are evaluated at runtime! Hence the unattractive specter of trying to mangle a runtime expression.
April 26, 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.
April 26, 2012
On 4/25/2012 9:42 PM, H. S. Teoh wrote:
> 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());
>
> ?

Give you two error messages, because default arguments wouldn't be allowed for delegates.
April 26, 2012
On Wed, Apr 25, 2012 at 10:08:26PM -0700, Walter Bright wrote:
> On 4/25/2012 9:42 PM, H. S. Teoh wrote:
> >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());
> >
> >?
> 
> Give you two error messages, because default arguments wouldn't be allowed for delegates.

No I mean the current behaviour. And I just checked: dmd compiles it fine, and prints 1 for both cases (which is wrong).


T

-- 
EMACS = Extremely Massive And Cumbersome System
April 26, 2012
On 4/25/2012 10:20 PM, H. S. Teoh wrote:
> On Wed, Apr 25, 2012 at 10:08:26PM -0700, Walter Bright wrote:
>> On 4/25/2012 9:42 PM, H. S. Teoh wrote:
>>> 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());
>>>
>>> ?
>>
>> Give you two error messages, because default arguments wouldn't be
>> allowed for delegates.
>
> No I mean the current behaviour. And I just checked: dmd compiles it
> fine, and prints 1 for both cases (which is wrong).

Hence the problem :-)
April 26, 2012
On 4/26/12 11:44 AM, Walter Bright 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?)

I don't understand the relationship between two delegate types being the same and thus sharing the same implementation for default arguments for *different instances* of a delegate with the same type.

Maybe a bug in how it's currently implemented?
April 26, 2012
On 4/25/2012 10:29 PM, Ary Manzana wrote:
> I don't understand the relationship between two delegate types being the same
> and thus sharing the same implementation for default arguments for *different
> instances* of a delegate with the same type.
>
> Maybe a bug in how it's currently implemented?

If you call a delegate directly, then the default arguments work. If you call it indirectly, the default arguments won't work.
« First   ‹ Prev
1 2 3 4 5
Top | Discussion index | About this forum | D home