Function pointers/delegates default args were stealth removed?

Timon Gehr timon.gehr at gmx.ch
Mon Aug 27 07:55:46 PDT 2012


On 08/27/2012 03:23 PM, kenji hara wrote:
> I think that the function type and its mangled name must not contain
> the default args, then I can agree with Walter at the point.
>
> But, I think the variables of function pointers and delegates still
> can have default args as a part of their declarations.
> (It will be a part of VarDeclaration, not a part of TypeFunction)
> In following case, the default arg looks like a part of the type, but
> actually is a part of the declaration of fp.
>
> void function(int n = 10) fp;
> pragma(msg, typeof(fp));  // should print void function(int), because
> default args are not a part of type.
> fp();  // I think this can be allowed, because fp can remember that
> the first parameter has the default argument.
>
> But, it seems to me there are some corner cases.
>
>    // fp will *inherit* default args from its initializer, or not?
>    auto fp = (int n = 10){}
>

I assume we'd like it to do that lest the user has to supply the type
explicitly. It might lead to funny interactions though.

>    // what is the actual default arg of fp?
>    void function(int n = 10) fp1 = (int n = 20){}
>    void function(int n) fp2 = (int n = 30){}
>    void function(int n = 40) fp3 = (int n){}
>

The declared default arg overrides.

I'd suggest to make it an error when it can be fixed by leaving out the
default argument from a function literal occurring within the
expression.

>    // fp has ambiguous default arg, or has no default arg?
>    auto fp1 = some_runtime_condition ? (int n = 10){} : (int n = 20){} ;
>    // more complicated case, first defarg is same, then it will be *inherited*?
>    auto fp2 = some_runtime_condition ? (int n = 10, string s =
> "hello"){} : (int n = 10, string s = "world"){} ;
>

I'd say, never attempt to combine default arguments in any way.

It is not required to make it a hard error in all cases, because the
error will occur when the user attempts to rely upon the non-existent
default argument. By the argument from above, both of those should
be in error.


>    int function(int n = 10) fp;   // default arg of the first parameter is 10
>    fp = (int n = 20){ return n; }  // function literal's default arg
> will be ignored (in my opinion), is this expected?
>

This specific expression should be in error, but it should probably be
possible to do

static int foo(int n=10){ return n; }
int function(int = 20) fp = &foo;

The reason why this should work is because adding a default argument
should be as unlikely as possible to break existing code.


>    // returning function pointer/delegate type can have default args?
>    int delegate(int n = 10) foo(int x) { ... }
>

Seems easy enough to support for consistency.

> If we can take agreements each other about them, it may be supported.
>

I personally do not rely on them.



More information about the Digitalmars-d mailing list