this is almost a workaround for the lack of named parameters

Jacob Carlborg doob at me.com
Sun Mar 24 03:14:43 PDT 2013


On 2013-03-24 03:05, timotheecour wrote:
> 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)

Absolutely.

> 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))

What would be nice is to be able to do like this:

struct Options{int x; int y=1; int z=2;}
auto fun(Options options);

fun(y: 2, z: 45, x: 4);

Then add opDispatch to Options to soak up any varialble not found in 
Options:

struct Options
{
     int x;
     int y = 1;
     int z = 2;

     private Variant[string] values;

     void opDispatch (string name, T) (T value)
     {
         values[name] = Variant(value);
     }
)

fun(y: 2, foo: "asd", bar: 4.0, z: 45, x: 4);

x, y, z are mapped to the variables in Options. foo and bar are handled 
by opDispatch.

See my proposal for anonymous structs:

http://forum.dlang.org/thread/kfbnuc$1cro$1@digitalmars.com

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list