Template sequence parameter and default value

Jaime benjamin.i.mccann at gmail.com
Thu Jan 27 03:19:59 UTC 2022


On Thursday, 27 January 2022 at 02:49:22 UTC, Andrey Zherikov 
wrote:
> What is the best way to emulate a default value for template 
> sequence parameter of a function?
>
> I want something like this:
> ```d
> void foo(MODULES... = __MODULE__)() {}
>
> // these should work:
> foo!(module1, module2);
> foo!(module1);
> foo();          // this should use current module (__MODULE__) 
> according to default value
> ```

You can accomplish this by heading off the template sequence 
parameter with several default template parameters. If you need 
them all under one name, you can recombine them in the function 
body with std.meta.AliasSeq, the effective "kind" of a template 
sequence parameter.

Example:

```d
void foo(string FirstModule = __MODULE__, RestModules...)() {
     alias Modules = AliasSeq!(FirstModule, RestModules);
     // things
}

// foo!(module1, module2) => alias Modules = (module1, module2)
// foo!() => alias Modules = (__MODULE__)
```




More information about the Digitalmars-d-learn mailing list