Manipulating alias sequences

Paul Backus snarwin at gmail.com
Mon Jul 15 14:50:20 UTC 2019


On Monday, 15 July 2019 at 13:40:29 UTC, Ben Ogles wrote:
> Now I want to extend it so that a caller can specify the values 
> of only some of the parameters. I tried using a static foreach 
> instead of the staticMap function. But I can't modify AliasSeq 
> values.
>
> alias args = AliasSeq!(0, 0);
> static foreach (idx, val; args) {
>   static if (user_defined_function_exists_for_arg!(idx)) {
>     args[idx] = user_defined_function(); // cannot modify tuple
>   } else {
>     args[idx] = gen_rand_integral!(typeof(val)); // cannot 
> modify tuple
>   }
> }
>
> How do I build up an argument tuple at compile time where some 
> values are randomly generated and others are given through some 
> user defined function (most likely automatically discovered by 
> a naming convention)?

Use a run-time Tuple instead of an AliasSeq:

import std.typecons;
auto args = tuple(0, 0);
static foreach (idx, val; args) {
     static if (user_defined_function_exists_for_arg!idx) {
         args[idx] = user_defined_function();
     } else {
         args[idx] = gen_random_integral!(typeof(val));
     }
}

Then use `fun(args.expand)` to pass the members of the tuple as 
separate arguments to `fun`.


More information about the Digitalmars-d-learn mailing list