digitalmars.D.learn - Make a type tuple from an array
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (9/9) Apr 10 2015 Is there a way to turn an array (known at compile time) into a
- John Colvin (29/38) Apr 10 2015 For input ranges in general:
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (4/45) Apr 10 2015 Wow, thanks! I had to `import std.array : empty, front,
- John Colvin (7/56) Apr 10 2015 Haha, yeah... I forgot where they were, they aren't documented in
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (4/9) Apr 10 2015 I had seen the PR, but didn't mentally connect it with
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/3) Apr 10 2015 Why not use isStaticArray instead of isInputRange here?
- John Colvin (3/6) Apr 11 2015 Because that would be completely different. Static arrays aren't
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/6) Apr 11 2015 Ahh, my mistake.
- Dicebot (4/11) Apr 11 2015 Code generation. Doing string mixin for each element of the array
- John Colvin (3/16) Apr 11 2015 Generating case statements is a nice example use that doesn't
- Artur Skawina via Digitalmars-d-learn (11/35) Apr 11 2015 Neat, but very unreadable...
Is there a way to turn an array (known at compile time) into a TypeTuple? I want to do a static foreach over it, like this: foreach(field; fields) { static if(is(mixin("T." ~ field.name))) { ... } } Obviously that won't work, because we don't have a real static foreach yet. I already worked around it with a string mixin, but I'd like to know whether there's a cleaner way...
Apr 10 2015
On Friday, 10 April 2015 at 15:13:54 UTC, Marc Schütz wrote:Is there a way to turn an array (known at compile time) into a TypeTuple? I want to do a static foreach over it, like this: foreach(field; fields) { static if(is(mixin("T." ~ field.name))) { ... } } Obviously that won't work, because we don't have a real static foreach yet. I already worked around it with a string mixin, but I'd like to know whether there's a cleaner way...For input ranges in general: import std.range : isInputRange; template TypeTupleOf(TL...) if (TL.length == 1 && isInputRange!(typeof(TL[0]))) { import std.typetuple : TT = TypeTuple; enum r = TL[0]; static if (r.empty) alias TypeTupleOf = TT!(); else { enum f = r.front; alias TypeTupleOf = TT!( f, TypeTupleOf!( { auto tmp = r; tmp.popFront(); return tmp; }() ) ); } } unittest { import std.range : iota; foreach(i; TypeTupleOf!(iota(10))) { pragma(msg, i); } }
Apr 10 2015
On Friday, 10 April 2015 at 15:36:42 UTC, John Colvin wrote:On Friday, 10 April 2015 at 15:13:54 UTC, Marc Schütz wrote:Wow, thanks! I had to `import std.array : empty, front, popFront;`, but otherwise it works great. This should be in Phobos!Is there a way to turn an array (known at compile time) into a TypeTuple? I want to do a static foreach over it, like this: foreach(field; fields) { static if(is(mixin("T." ~ field.name))) { ... } } Obviously that won't work, because we don't have a real static foreach yet. I already worked around it with a string mixin, but I'd like to know whether there's a cleaner way...For input ranges in general: import std.range : isInputRange; template TypeTupleOf(TL...) if (TL.length == 1 && isInputRange!(typeof(TL[0]))) { import std.typetuple : TT = TypeTuple; enum r = TL[0]; static if (r.empty) alias TypeTupleOf = TT!(); else { enum f = r.front; alias TypeTupleOf = TT!( f, TypeTupleOf!( { auto tmp = r; tmp.popFront(); return tmp; }() ) ); } } unittest { import std.range : iota; foreach(i; TypeTupleOf!(iota(10))) { pragma(msg, i); } }
Apr 10 2015
On Friday, 10 April 2015 at 17:53:31 UTC, Marc Schütz wrote:On Friday, 10 April 2015 at 15:36:42 UTC, John Colvin wrote:Haha, yeah... I forgot where they were, they aren't documented in std.array (grrrr), so I just left it for someone else to find :pOn Friday, 10 April 2015 at 15:13:54 UTC, Marc Schütz wrote:Wow, thanks! I had to `import std.array : empty, front, popFront;`, but otherwise it works great.Is there a way to turn an array (known at compile time) into a TypeTuple? I want to do a static foreach over it, like this: foreach(field; fields) { static if(is(mixin("T." ~ field.name))) { ... } } Obviously that won't work, because we don't have a real static foreach yet. I already worked around it with a string mixin, but I'd like to know whether there's a cleaner way...For input ranges in general: import std.range : isInputRange; template TypeTupleOf(TL...) if (TL.length == 1 && isInputRange!(typeof(TL[0]))) { import std.typetuple : TT = TypeTuple; enum r = TL[0]; static if (r.empty) alias TypeTupleOf = TT!(); else { enum f = r.front; alias TypeTupleOf = TT!( f, TypeTupleOf!( { auto tmp = r; tmp.popFront(); return tmp; }() ) ); } } unittest { import std.range : iota; foreach(i; TypeTupleOf!(iota(10))) { pragma(msg, i); } }This should be in Phobos!Yup. I've mentioned it here: https://github.com/D-Programming-Language/phobos/pull/3128 but it's a bit of a complicated situation as a bunch of names are likely to change.
Apr 10 2015
On Friday, 10 April 2015 at 18:25:14 UTC, John Colvin wrote:I had seen the PR, but didn't mentally connect it with std.typetuple. Now I see it's intended as a replacement for it. Good that it won't get lost.This should be in Phobos!Yup. I've mentioned it here: https://github.com/D-Programming-Language/phobos/pull/3128 but it's a bit of a complicated situation as a bunch of names are likely to change.
Apr 10 2015
On Friday, 10 April 2015 at 15:36:42 UTC, John Colvin wrote:if (TL.length == 1 && isInputRange!(typeof(TL[0])))Why not use isStaticArray instead of isInputRange here?
Apr 10 2015
On Friday, 10 April 2015 at 22:55:23 UTC, Nordlöw wrote:On Friday, 10 April 2015 at 15:36:42 UTC, John Colvin wrote:Because that would be completely different. Static arrays aren't even input ranges...if (TL.length == 1 && isInputRange!(typeof(TL[0])))Why not use isStaticArray instead of isInputRange here?
Apr 11 2015
On Saturday, 11 April 2015 at 07:18:26 UTC, John Colvin wrote:Ahh, my mistake. Could somebody explain when this feature is needed?Why not use isStaticArray instead of isInputRange here?Because that would be completely different. Static arrays aren't even input ranges...
Apr 11 2015
On Saturday, 11 April 2015 at 09:05:19 UTC, Nordlöw wrote:On Saturday, 11 April 2015 at 07:18:26 UTC, John Colvin wrote:Code generation. Doing string mixin for each element of the array (won't work with plain foreach over array as it does runtime iteration)Ahh, my mistake. Could somebody explain when this feature is needed?Why not use isStaticArray instead of isInputRange here?Because that would be completely different. Static arrays aren't even input ranges...
Apr 11 2015
On Saturday, 11 April 2015 at 09:23:11 UTC, Dicebot wrote:On Saturday, 11 April 2015 at 09:05:19 UTC, Nordlöw wrote:Generating case statements is a nice example use that doesn't (necessarily) involve string mixins.On Saturday, 11 April 2015 at 07:18:26 UTC, John Colvin wrote:Code generation. Doing string mixin for each element of the array (won't work with plain foreach over array as it does runtime iteration)Ahh, my mistake. Could somebody explain when this feature is needed?Why not use isStaticArray instead of isInputRange here?Because that would be completely different. Static arrays aren't even input ranges...
Apr 11 2015
On 04/10/15 17:36, John Colvin via Digitalmars-d-learn wrote:On Friday, 10 April 2015 at 15:13:54 UTC, Marc Schütz wrote:Is there a way to turn an array (known at compile time) into a TypeTuple?For input ranges in general: import std.range : isInputRange; template TypeTupleOf(TL...) if (TL.length == 1 && isInputRange!(typeof(TL[0]))) { import std.typetuple : TT = TypeTuple; enum r = TL[0]; static if (r.empty) alias TypeTupleOf = TT!(); else { enum f = r.front; alias TypeTupleOf = TT!( f, TypeTupleOf!( { auto tmp = r; tmp.popFront(); return tmp; }() ) ); } }Neat, but very unreadable... import std.array, std.range : isInputRange, dropOne; template TypeTupleOf(alias R) if (isInputRange!(typeof(R))) { import std.typetuple : TT = TypeTuple; static if (R.empty) alias TypeTupleOf = TT!(); else alias TypeTupleOf = TT!(R.front(), TypeTupleOf!(R.dropOne())); } artur
Apr 11 2015