April 07, 2014
On Monday, 7 April 2014 at 19:47:24 UTC, Frustrated wrote:
> 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);

C# uses <name>:, like the follow.

void TestFun(int i, string s = "", bool b)
{
    //...
}

TestFun(i: 1, b: false);

Is there any reason not to use this syntax? It doesn't *seem* to conflict with anything else.
April 07, 2014
On Monday, 7 April 2014 at 20:00:39 UTC, Meta wrote:
> On Monday, 7 April 2014 at 19:47:24 UTC, Frustrated wrote:
>> 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);
>
> C# uses <name>:, like the follow.
>
> void TestFun(int i, string s = "", bool b)
> {
>     //...
> }
>
> TestFun(i: 1, b: false);
>
> Is there any reason not to use this syntax? It doesn't *seem* to conflict with anything else.

I like this a lot more. It looks similar to the associative array literal syntax, which is fitting.
April 07, 2014
While we're at it - I would also like to see the two forms of "variadic" named arguments:

1) Implicit Associative Array Version: works similar to Ruby

void foo(string[string] args...){
    foreach(k,v;args){
        writeln("%s = %s",k,v);
    }
}

foo("a":"b","c":"d");

prints:
a = b
c = d



2) Templated Version: not sure about the declaration syntax. Maybe:

void foo(T[...])(T args){
    foreach(field;__traits(allMembers,T)){
        writeln("%s(%s) = %s",field,typeof(__traits(getMember,args,field)).stringof,__traits(getMember,args,field));
    }
}

foo(a:1,b:"2");

prints:
a(int) = 1
b(string) = 2
April 08, 2014
On Sat, 05 Apr 2014 21:26:17 -0400, Nick Sabalausky wrote:

> I realize this isn't the time for such a thing to be added to D, but I thought I'd put the idea out there, FWIW:
> 
> I find myself frequently needing to design APIs that work like this:
> 
>      func( [optionalFoo], [optionalBar] )
> 
> Typically, that's not permitted in C-style languages, including D. They only allow this:
> 
>      func( [optionalFoo, [optionalBar]] )
> 

static structs can be initialized by name, maybe that syntax could be expanded upon?

void foo( struct{ int a = 1; double b = 3.14; } ) { .. }
...
foo( {b: 2} );

that or named tuples with an init value set?
1 2
Next ›   Last »