`with` across function calls

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Jan 18 23:38:39 UTC 2019


On Friday, 18 January 2019 at 14:51:35 UTC, Steven Schveighoffer 
wrote:
> On 1/18/19 7:35 AM, Nicholas Wilson wrote:
>> The example above is a bit simplified, the call that I'm 
>> trying to not pass `g`directly to looks like
>> 
>> q.enqueue!(f!(args))(arg_set1)(arg_set2);
>> 
>> and the function within that needs `g` also needs `f`. The 
>> expected usage is a whole lot of those calls all in one spot 
>> with different `f`, `args`, `arg_set1`, `arg_set2` I really 
>> don't want the user to have to repeat themselves anymore than 
>> absolutely necessary.
>
> All you seem to be looking for is a context with specified 
> default parameters. Why not make a struct?
>
> auto context = With!g;
>
> context.bar();
>
> With opDispatch and introspection, this should be doable.
>
> -Steve

So `q` is a struct with a number of methods, calls to its other 
methods (which won't need `g`) will be interspersed between calls 
that need `g`. I can't add `g` to `q`, because that breaks the 
logical objects, they are wrappers of opaque classes, and `g` has 
nothing to do with `q` except that the call 
`q.enqueue!(f!(args))(arg_set1)(arg_set2);` needs a `g` to create 
a object based on f and arg_set2.

I'm not sure that would improve the legibility of the code.



More information about the Digitalmars-d mailing list