this is almost a workaround for the lack of named parameters

Timothee Cour thelastmammoth at gmail.com
Tue Mar 26 20:29:15 PDT 2013


>>Interesting, the "doesn't not support passing the arguments out of order." can be seen either as a (temporary or not) implementation >>limitation OR as a feature.

Named parameters are only interesting if we can skip some optional parameters.
This allows the python-like syntax of specifying only a subset of
parameters; otherwise this isn't very interesting. This is used
heavily in python and makes code
* self-documenting
* avoids DRY (don't specify unused params)
* avoids boilerplate of introducing auxiliary option structs and fields to it

Here are just 3 examples that hopefully will convince some that named
params are useful and not ugly.

----
//inspired from python's matplotlib; many more options configurable,
which are set to reasonable defaults
plot(x=1,y=2,color='red',width=3);

//here's another one (cf inspired by scons / waf build tools in python)
compile(input=["foo.cpp"] , run=true, debug=true, ldflags="-lcurl",
output_dir="build");

//other example: setting optional params in a classifier
trainSVM(input=X, labels=Y, C=1000, crossValidate=true, loss=squareHingeLoss)
----

Additionally, we should be able to provide arguments out of order and
check at compile time that there are no duplicates and no unexistant
fields. The code i posted with support for
named!plot.x(2).color("red").width(3)
did that, and that wasn't hard. So hopefully it won't be easy to
support as well skipping, reordering and CT checking with @Michel
Fortin's shiny syntax.

Here are some design decisions to make: given a function void fun(int
a,int b=1, int c=2);

* whether to support mixing named and non-named arguments in 1 call
(python does)
fun(0,b=1)

* whether to allow both calling conventions:
fun(0,1)
fun(0,b=1)

* whether to allow both calling conventions:
fun(0,1)
fun(0,b=1)

* whether to tag functions callable by named params via attribute
(check is done at CT): that'll indicate clearly we want to freeze
names in the API
@named void fun(int a,int b=1, int c=2);

* alternatively to above, whether to use ':' instead of '=' in
function declaration
void fun(int a,int b:1, int c:2); //now it's only callable by name

* whether to support skipping and reordering optional args ; I argued
we need to for this to be useful enough

Timothee Cour


More information about the Digitalmars-d mailing list