As discussed in DConf2015: Python-like keyword arguments
Liran Zvibel via Digitalmars-d
digitalmars-d at puremagic.com
Fri May 29 10:46:10 PDT 2015
On Friday, 29 May 2015 at 15:16:56 UTC, Jonathan Crapuchettes
wrote:
> Two other ways to implement the concept. The first works with
> optional
> arguments and the other requires all arguments.
>
> unittest
> {
> alias arg1 = namedArguments!("arg1", string);
> alias arg2 = namedArguments!("arg2", int);
>
> void namedArgsFunc(T...)(T arguments)
> {
> const vars = parseArguments!()(arguments);
> const string one = vars.arg1;
> const int two = vars.arg2;
> }
> namedArgsFunc(arg1(""), arg2(42));
> namedArgsFunc(arg2(56), arg1("fred"));
> }
...
> Jonathan
This is a very interesting approach, but the problem is that the
person writing the function has to work to give support.
I think we should try to create a wrapper function taking the
original function as (alias F) that leverages
ParameterIdentifierTuple , ParameterTypeTuple and
ParameterDefaultValueTuple to generate the namedArguments
automatically.
Also, we could actually make this a Functor/Callback object that
has already created members that give you easy access to the
types of the named arguments. This way, we can even conceive a
way to return a "partial" function with enough arguments, and
only really call it when all the keyword arguments were actually
provided (or enough of them were provided and the rest have
provided a default value).
This will allow us to create a concise API that is convenient,
and strong.
Liran
More information about the Digitalmars-d
mailing list