Dream Feature Regarding Default Arguments

Frustrated Who at where.com
Mon Apr 7 12:47:23 PDT 2014


On Monday, 7 April 2014 at 17:46:44 UTC, w0rp wrote:
> On Sunday, 6 April 2014 at 14:47:28 UTC, JN wrote:
>> Wouldn't it be better to have named parameters like in Python 
>> or
>> C#? That way you could call the function like:
>>
>> func(foo=myFoo) or func(bar=myBar) or func(foo=myFoo, 
>> bar=myBar)
>> and there would be no ambiguity.
>
> Named parameters would be nice to have. I'm not sure how you 
> would implement them in D, though.

it would be relatively easy.

void myfunc(name = int x) { }

instead of

void myfunc(int x) { }

then

myfunc(name = 4);

or one could simply use the variable name

void myfunc(int x) { }

myfunc(x = 3);


Of course assignments may not be valid, one could use := instead.

myfunc(x := 3);



One could build a template to do it how were but it would require 
calling the function as a string,

e.g., template is passed the call as a string. The template gets 
the name of the function, looks up the parameter names, parses 
the arguments and generates the proper call string which is then 
mixed in.

e.g., Named(q{myfunc(x := 3)}); => myfunc(3);





More information about the Digitalmars-d mailing list