As discussed in DConf2015: Python-like keyword arguments
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Fri May 29 05:27:02 PDT 2015
On 2015-05-29 00:35, Atila Neves wrote:
> I might do a blog post on this, but here's some POC code:
>
> import std.stdio;
> import std.range;
> import std.typetuple;
> import std.traits;
> import std.conv;
>
>
> struct Foo { int i; }
> struct Bar { int i; }
> struct Baz { int i; }
>
>
> void func(Foo foo, Bar bar, Baz baz) {
> writeln("foo is ", foo);
> writeln("bar is ", bar);
> writeln("baz is ", baz);
> }
>
>
> auto getStrArgs(alias F, T...)() {
> string[] strArgs;
>
> foreach(i, ParamType; ParameterTypeTuple!F) {
> enum index = staticIndexOf!(ParamType, T);
>
> static if(index != -1) {
> strArgs ~= "args[" ~ index.to!string ~ "]";
> } else {
> strArgs ~= ParamType.stringof ~ ".init";
> }
> }
>
> return strArgs;
> }
>
> auto kwargs(alias F, T...)(T args) {
> enum strArgs = getStrArgs!(F, T);
> mixin("return F(" ~ strArgs.join(",") ~ ");");
> }
>
> void main() {
> kwargs!func(Bar(2), Baz(3), Foo(1));
> kwargs!func(Baz(3), Foo(1));
> }
Here's another solution [1].
And here's an implementation with language support which allows named
arguments but not reordering the arguments [2]. Originally implemented
by Michel Fortin.
[1]
https://github.com/jacob-carlborg/mambo/blob/master/mambo/util/Reflection.d#L135
[2] https://github.com/jacob-carlborg/dmd/tree/named_parameters
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list