Creating Tuple or AliasSeq

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 7 00:46:40 PDT 2017


On 04/06/2017 10:43 PM, ANtlord wrote:
> Hello! I've got an issue related to making a Tuple or AliasSeq using
> income template arguments. I want to create template makes an array of
> objects from array of arrays have different sizes and different types of
> values.

Here is a solution:

auto objectFactory(ObjectType, Args...)(Args args) {
     import std.algorithm : cartesianProduct, map;
     import std.array : array;

     return cartesianProduct(args).map!(a => ObjectType(a.expand)).array;
}

struct Pair {
     int number;
     string name;
}

import std.stdio;

void main() {
     auto pairKit = objectFactory!(Pair)([1, 2], ["qwe", "asd"]);
     auto pairSet = pairKit;
     writeln(pairSet);
}

Ali



More information about the Digitalmars-d-learn mailing list