with(auto x = ...)

Kapps via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 26 00:28:43 PDT 2015


On Friday, 24 July 2015 at 15:01:29 UTC, Adam D. Ruppe wrote:
> On Friday, 24 July 2015 at 14:48:30 UTC, Marc Schütz wrote:
>> But I fail to see the relation to named parameters?
>
> You can make your parameters into a struct or tuple and fill 
> them in with normal assignment. The with(auto) thing will 
> conveniently limit the scope of the temporary argument object 
> and give a bit of syntax sugar for it:
>
> with(auto args = ParameterTypeTuple!foo) {
>    argname = cool;
>    argname2 = whatever;
>    foo(args);
> }
>
>
> .....ironically, given the other thread about statements as 
> expressions where i said 'meh', this is actually a decent case 
> for them too, to get the return value of foo out of that scope 
> while still allowing auto.
>
> But that's a separate issue, the main point here is just that 
> you can use it to prepare the arguments with a bit of sugar.

The with statement is one where I think it would be interesting 
to make it an expression.

For named parameters (admittedly, I find this one a bit ugly):
foo(with(ParameterTypeTuple!foo) {
     abc = 2,
     def = 3
});

Or just:
auto args = with(ParameterTypeTuple!foo) {
     abc = 2,
     def = 3
};
foo(args);


For initialization:
auto a = with(new FooBar()) {
     name = "Foo",
     bar = 3
};

Or:
with(new Thread(&foo) {
     isDaemon = true
}).start();


More information about the Digitalmars-d mailing list