digitalmars.D.learn - Passing Elements of A Static Array as Function Parameters
- =?UTF-8?B?Tm9yZGzDtnc=?= (6/6) Sep 13 2015 If I have a static array `x` defined as
- Meta (32/38) Sep 14 2015 You could turn it into a Tuple and use the `expand` method to get
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (2/22) Sep 14 2015 BTW: What about .tupleof? Isn't that what should be used here?
- Meta (2/3) Sep 14 2015 I don't believe .tupleof works for arrays.
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/34) Sep 14 2015 Is there a reason why such a common thing isn't already in
- Meta (3/5) Sep 14 2015 I guess nobody's really needed that functionality before. It
If I have a static array `x` defined as enum N = 3; int[N] x; how do I pass it's elements into a variadic function f(T...)(T xs) if (T.length >= 3) ?
Sep 13 2015
On Monday, 14 September 2015 at 05:18:00 UTC, Nordlöw wrote:If I have a static array `x` defined as enum N = 3; int[N] x; how do I pass it's elements into a variadic function f(T...)(T xs) if (T.length >= 3) ?You could turn it into a Tuple and use the `expand` method to get a TypeTuple (AliasSeq). import std.typecons; import std.typetuple; import std.stdio; template genTypeList(T, size_t n) { static if (n <= 1) { alias genTypeList = T; } else { alias genTypeList = TypeTuple!(T, genTypeList!(T, n - 1)); } } auto asTuple(T, size_t n)(ref T[n] arr) { return Tuple!(genTypeList!(T, n))(arr); } void test(T...)(T xs) { writeln("Length: ", T.length, ", Elements: ", xs); } void main() { int[5] a = [0, 1, 2, 3, 4]; test(a); //Length: 1, Elements: [0, 1, 2, 3, 4] test(a.asTuple.expand); //Length: 5, Elements: 01234 }
Sep 14 2015
On Monday, 14 September 2015 at 07:05:23 UTC, Meta wrote:You could turn it into a Tuple and use the `expand` method to get a TypeTuple (AliasSeq). import std.typecons; import std.typetuple; import std.stdio; template genTypeList(T, size_t n) { static if (n <= 1) { alias genTypeList = T; } else { alias genTypeList = TypeTuple!(T, genTypeList!(T, n - 1)); } } auto asTuple(T, size_t n)(ref T[n] arr) { return Tuple!(genTypeList!(T, n))(arr); }BTW: What about .tupleof? Isn't that what should be used here?
Sep 14 2015
On Monday, 14 September 2015 at 08:56:43 UTC, Per Nordlöw wrote:BTW: What about .tupleof? Isn't that what should be used here?I don't believe .tupleof works for arrays.
Sep 14 2015
On Monday, 14 September 2015 at 07:05:23 UTC, Meta wrote:You could turn it into a Tuple and use the `expand` method to get a TypeTuple (AliasSeq). import std.typecons; import std.typetuple; import std.stdio; template genTypeList(T, size_t n) { static if (n <= 1) { alias genTypeList = T; } else { alias genTypeList = TypeTuple!(T, genTypeList!(T, n - 1)); } } auto asTuple(T, size_t n)(ref T[n] arr) { return Tuple!(genTypeList!(T, n))(arr); } void test(T...)(T xs) { writeln("Length: ", T.length, ", Elements: ", xs); } void main() { int[5] a = [0, 1, 2, 3, 4]; test(a); //Length: 1, Elements: [0, 1, 2, 3, 4] test(a.asTuple.expand); //Length: 5, Elements: 01234 }Is there a reason why such a common thing isn't already in Phobos? If not what about adding it to std.typecons : asTuple
Sep 14 2015
On Monday, 14 September 2015 at 09:09:27 UTC, Per Nordlöw wrote:Is there a reason why such a common thing isn't already in Phobos? If not what about adding it to std.typecons : asTupleI guess nobody's really needed that functionality before. It might be an interesting addition to std.array.
Sep 14 2015