Named arguments

Andrea Fontana nospam at example.com
Wed Oct 25 10:32:19 UTC 2017


On Wednesday, 25 October 2017 at 07:46:49 UTC, Andrey wrote:
> good alternative, I already forgot about the power of structs 
> after Java.

It would be a very good solution if we can init a struct using 
its named field. Something like:

struct S
{
   int a;
   int b;
   int c;
}

S test = { a:1, b:4; };

So:

void myFunc(S param);  =>   myFunc({a:1, b:3});

I know: you say that this has trouble with overloading:

void myFunc(S param);
void myFunc(T param);

But the same goes for this:
void f(float i) { writeln("float"); }
void f(double i) { writeln("double"); }
f(1);

And simply it won't compile. (for template arguments too)
You can solve this doing: f(1.0f); or f(1.0);

In the same way you can solve first problem calling:
myFunc({a:1, b:3}.to!S) or  myFunc(S{a:1, b:3}) or 
myFunc(cast(S){a:1, b:3}) or some other exotic syntax you want 
add to language, if needed (rarely, i guess).

And the same goes for:

auto blah = {a:1, b:2};



More information about the Digitalmars-d mailing list