Will D ever get optional named parameters?

"岩倉 澪" via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 14 14:21:21 PDT 2014


On Tuesday, 14 October 2014 at 03:34:06 UTC, Ary Borenszweig 
wrote:
> One simple thing we did in Crystal is to allow invoking a 
> function with named arguments only for arguments that have a 
> default value. For example:
>
> void foo(int x, int y = 2, int z = 3) { ... }
>
> foo(x, y: 10);
> foo(x, y: 10, z: 20);
> foo(x, z: 30)
>
> But not this:
>
> foo(x: 10)
>
> The logic behind this is that named arguments are usually 
> wanted when you want to replace one of the default values while 
> keeping the others' defaults. You could specify names for 
> arguments that don't have a default value, but that only gives 
> a small readability aid. Changing a default value in the middle 
> is a new feature.
>
> This greatly simplifies the logic, since parameter reordering 
> can only happen for names that have default values and you can 
> always fill the gaps. Also, default values can also appear last 
> in a function signature.
Another thought I had is that an alternative could be to have 
some special syntax to say "use the default for this parameter."
I do not have experience implementing languages so perhaps I am 
wrong, but it seems like it should be possible for the compiler 
to get the default value and replace some placeholder with it.

Something like:
void foo(int a = 42, int b = 0){}
foo(@default, 7); //rewritten to foo(42, 7);


More information about the Digitalmars-d mailing list