this is almost a workaround for the lack of named parameters
timotheecour
timothee.cour2 at gmail.com
Sat Mar 23 19:05:54 PDT 2013
I updated
https://github.com/timotheecour/dtools/blob/master/dtools/util/functional.d
to incorporate Jacob Carlborg's idea (thanks for the
working+clean code! let me know if attribution is ok)
auto a=named!fun.z(3).x(4).call();
and then simplified it a bit to:
auto a=named!fun.z(3).x(4)();
It's now looking pretty close to the ideal D syntax:
auto a=fun(z:3,x:4);
Notes:
auto a=fun(z=3,x=4);
doesn't seem like a good syntax compared to 'z:3,x:4', because
z=3 could be ambiguous with an expression assigning 3 to z when z
is in scope.
> I find it a pain to have to pollute namespaces with extra enums
> (classes), just to get readability/safety.
> plot(data, linewidth = 5)
> to be clean, clear, and more concise.
I agree.
In fact, I think we should push that named syntax
'named!fun.z(3).x(4)();' in std.functional once finalized, and
start using it in phobos ASAP, it makes things more self
documenting.
If 'fun(z:3,x:4)' syntax later comes to D, it'd be trivial to
write a tool that automatically changes the named!fun syntax in
people's / phobos' source code to use the new cleaner syntax.
main benefit IMO: prevent boilerplate code like this:
struct Options{int x; int y=1; int z=2;}
auto fun(Options options);
Options options;
options.x=4;
options.z=3;
auto a=fun(options);
(or similar with param expansion: auto
a=fun(options.x,options.y,options.z))
thanks for your comments!
More information about the Digitalmars-d
mailing list