Function pointers/delegates default args were stealth removed?

kenji hara k.hara.pg at gmail.com
Mon Aug 27 06:23:54 PDT 2012


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){}

  // 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){}

  // 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"){} ;

  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?

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

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

Kenji Hara

2012/8/27 Walter Bright <newshound2 at digitalmars.com>:
> On 8/27/2012 1:08 AM, Manu wrote:
>>
>> Does the bug report actually demonstrate how it was causing anybody any
>> problems? It seemed a rather contrived scenario that just illustrated that
>> there
>> was a bug.
>
>
> It was probably reduced from a larger scenario. Reduced bugs usually look
> pretty twisted.
>
>
>> Also, I think it could be fixed so the scenario in the bug report worked
>> as
>> expected (I still don't understand why it did't work in the first place).
>
>
> Because the two types were considered to be the same, only different.
>
> ----------------------------------
>
> Please post a canonical example of how you use this, so we can think of an
> alternative.


More information about the Digitalmars-d mailing list