Template sequence parameter and default value
Andrey Zherikov
andrey.zherikov at gmail.com
Thu Jan 27 15:21:31 UTC 2022
On Thursday, 27 January 2022 at 03:19:59 UTC, Jaime wrote:
> 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__)
> ```
Unfortunately `string FirstModule` doesn't work if I specify the
module: https://run.dlang.io/is/BZd0KB
The closest solution I have is this:
```d
void foo(MODULES...)()
{
writeln(MODULES.stringof);
}
alias foo(string MODULE = __MODULE__) = foo!(mixin(MODULE));
void main()
{
writeln(1);
foo();
writeln(2);
foo!(onlineapp);
writeln(3);
foo!(onlineapp,onlineapp);
}
```
It prints this:
```
1
tuple(module onlineapp)
2
tuple(module onlineapp)
3
tuple(module onlineapp, module onlineapp)
```
So is it possible to get rid of the alias?
More information about the Digitalmars-d-learn
mailing list