The programmer sometimes needs to instantiate mixin templates with the same arguments repeatedly, like:
mixin template A(int x, float y) {
// ...
}
mixin template B(int x, float y) {
// ...
}
I propose to shorten the above code with the following syntax:
template composite(int x, float y) {
mixin A {
// ...
}
mixin B {
// ...
}
}
// ...
struct Test {
mixin composite(1, 2.0);
}
Comment #2 by destructionator — 2018-12-31T14:29:32Z
You can just define a third template that mixes in the first two.
mixin template A(int x, float y) {
// ...
}
mixin template B(int x, float y) {
// ...
}
mixin template composite(int x, float y) {
mixin A!(x, y);
mixin B!(x, y);
}
struct Test {
mixin composite!(1, 2.0);
}
That works today.
Comment #3 by porton — 2018-12-31T14:32:00Z
This does not solve my problem.
I want the possibility to _selectively_ (at my disposal, that is both or only A or only B) add mixins A or B without the necessity to pass template parameters two times. Your example does not solve this problem.
Comment #4 by destructionator — 2018-12-31T14:38:13Z
You can also save template argument lists as a separate alias and reuse them.
// save the args as a new name to reuse
import std.meta;
alias MyArgs = AliasSeq!(1, 2.0);
struct Test {
mixin A!MyArgs;
mixin B!MyArgs;
}