Named arguments

jmh530 john.michael.hall at gmail.com
Tue Oct 24 23:27:02 UTC 2017


On Tuesday, 24 October 2017 at 20:36:00 UTC, H. S. Teoh wrote:
> [snip]
> it would be more self-documenting if written as:
>
> 	// (Hypothetical syntax)
> 	auto x = foo(width: 1, height: 2, userId: 3);
>

What about something that's a little uglier, but could be done 
with D magic?

For instance:
auto x = foo!(["width", "height", "userId"])(1, 2, 3)

So for instance, you could have some original function, like below

@namedParam("width", "height", "userId") auto x = _foo(int width, 
int height, UserId userId) { }

and then some D magic inserts a new function that is like

auto foo(string[] names)(...) { }

and then processes names, mixing in the correct named/typed 
variables and then returning a call to _foo with those variables.


More information about the Digitalmars-d mailing list