digitalmars.D.learn - Variadic Mixin/Template classes?
- Chirs Forest (24/24) Nov 25 2017 I'd like to make a class that takes multiple template types (1 -
- vit (22/46) Nov 25 2017 import std.meta : staticMap;
- Chirs Forest (2/26) Nov 25 2017 thankyou!
I'd like to make a class that takes multiple template types (1 -
several) which can hold an array/tuple of a second class that are
instantiated with those types.
class Bar(T) {
T bar;
}
class Foo(T[]){ // not sure how to take variadic types here?
Bar!(?)[] bars; //not sure how I'd define an array/tuple of
multiple types
this(){
foreach(int i, b; bars) b = new Bar!T[i];
}
}
void main(){
auto foo = new Foo!(string, int, string, ubyte[2]);
foo.bars[0].bar = "hello";
foo.bars[1].bar = 23;
foo.bars[2].bar = "hello";
foo.bars[3].bar[0] = 88;
foo.bars[3].bar[1] = 99;
auto foo2 = new Foo!(ubyte, string);
foo.bars[0].bar = 9;
foo.bars[1].bar = "world";
}
Nov 25 2017
On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote:
I'd like to make a class that takes multiple template types (1
- several) which can hold an array/tuple of a second class that
are instantiated with those types.
class Bar(T) {
T bar;
}
class Foo(T[]){ // not sure how to take variadic types here?
Bar!(?)[] bars; //not sure how I'd define an array/tuple of
multiple types
this(){
foreach(int i, b; bars) b = new Bar!T[i];
}
}
void main(){
auto foo = new Foo!(string, int, string, ubyte[2]);
foo.bars[0].bar = "hello";
foo.bars[1].bar = 23;
foo.bars[2].bar = "hello";
foo.bars[3].bar[0] = 88;
foo.bars[3].bar[1] = 99;
auto foo2 = new Foo!(ubyte, string);
foo.bars[0].bar = 9;
foo.bars[1].bar = "world";
}
import std.meta : staticMap;
class Bar(T) {
T bar;
}
class Foo(Ts...){
staticMap!(Bar, Ts) bars;
this(){
static foreach(i, alias T; Ts) bars[i] = new Bar!T;
}
}
void main(){
auto foo = new Foo!(string, int, string, ubyte[2]);
foo.bars[0].bar = "hello";
foo.bars[1].bar = 23;
foo.bars[2].bar = "hello";
foo.bars[3].bar[0] = 88;
foo.bars[3].bar[1] = 99;
auto foo2 = new Foo!(ubyte, string);
foo2.bars[0].bar = 9;
foo2.bars[1].bar = "world";
}
Nov 25 2017
On Saturday, 25 November 2017 at 10:08:36 UTC, vit wrote:On Saturday, 25 November 2017 at 09:52:01 UTC, Chirs Forest wrote:thankyou![...]import std.meta : staticMap; class Bar(T) { T bar; } class Foo(Ts...){ staticMap!(Bar, Ts) bars; this(){ static foreach(i, alias T; Ts) bars[i] = new Bar!T; } } void main(){ auto foo = new Foo!(string, int, string, ubyte[2]); foo.bars[0].bar = "hello"; foo.bars[1].bar = 23; foo.bars[2].bar = "hello"; foo.bars[3].bar[0] = 88; foo.bars[3].bar[1] = 99; auto foo2 = new Foo!(ubyte, string); foo2.bars[0].bar = 9; foo2.bars[1].bar = "world"; }
Nov 25 2017








Chirs Forest <CF chrisforest.com>