From [Tuple!(A,B), ...] to Tuple!(A[], B[])

Simen Kjærås simen.kjaras at gmail.com
Mon Feb 17 11:48:48 UTC 2020


On Monday, 17 February 2020 at 11:07:33 UTC, foozzer wrote:
> Hi all,
>
> There's something in Phobos for that?
>
> Thank you

import std.meta : staticMap;
import std.typecons : Tuple;

// Turn types into arrays
alias ToArray(T) = T[];
// Leave everything else the same
alias ToArray(T...) = T;
// Now apply the above to each element of the Tuple template args:
alias ToArrayTuple(T : Tuple!U, U...) = 
Tuple!(staticMap!(ToArray, U));

unittest {
     alias A = Tuple!(int, string);
     assert(is(ToArrayTuple!A == Tuple!(int[], string[])));
     alias B = Tuple!(int, "a", string, "b");
     assert(is(ToArrayTuple!B == Tuple!(int[], "a", string[], 
"b")));
}

--
   Simen


More information about the Digitalmars-d-learn mailing list