What to do about default function arguments

Don Clugston dac at nospam.com
Thu Apr 26 03:31:56 PDT 2012


On 26/04/12 12:11, Timon Gehr wrote:
> On 04/26/2012 11:46 AM, Don Clugston wrote:
>> On 26/04/12 11:28, Timon Gehr wrote:
>>> On 04/26/2012 10:51 AM, Don Clugston wrote:
>>>> On 26/04/12 05:44, 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 think it is a mistake to allow default arguments in function pointers
>>>> and delegates (it's OK for delegate literals, there you have the
>>>> declaration).
>>>
>>> The parenthesised part is in conflict with your other statement.
>>
>> No it doesn't. A default argument is a delegate literal is part of the
>> declaration, not part of the type.
>
> If types cannot specify default arguments, then those will be thrown
> away right away, because what is later called is based on the type of
> the delegate and not on the implicit function declaration that has its
> address taken. What is the point of allowing it if it cannot be used?

Fair point. It could be used in the case where it is called at the point 
of declaration (I do that a fair bit), but it's pretty much useless 
because it is clearer code to put the default parameter in the call.

int m = (int a, int b = 3){ return a+b;}(7);

Point conceded. So default arguments should be disallowed in delegate 
literals as well.


More information about the Digitalmars-d mailing list