digitalmars.D.learn - Generating members from parameter tuple
- Brad Anderson (26/26) Aug 18 2013 import std.typetuple;
- Brad Anderson (3/29) Aug 19 2013 For got to add a B!(Si, Sf) somewhere (I had a main() but
- John Colvin (3/29) Aug 19 2013 Take a look at the implementation of std.typetuple.Tuple, it does
- Brad Anderson (3/37) Aug 19 2013 I actually looked there first but it parses the types to support
- Dicebot (7/8) Aug 19 2013 I remember asking Andrei on IRC about feature as "declaration
- Brad Anderson (4/13) Aug 19 2013 Ok, that helps me understand how to do it with recursion and
- Dicebot (5/9) Aug 19 2013 Yes, if you can stick to semantics close to built-in value
import std.typetuple; struct S(T...) { alias Types = T; } alias Si = S!(int, short); alias Sf = S!(float, double); template STypes(STy) { alias STypes = STy.Types; } struct B(Ss...) { alias CombinedTypes = NoDuplicates!(TypeTuple!(staticMap!(STypes, Ss))); // I'd like to create a member here for every type in CombinedTypes // that is an array of each type. e.g., // int[] _intArray; // short[] _shortArray; // float[] _floatArray; // double[] _doubleArray; // // I don't believe the names matter because I'd probably want // to use a templated method that returns the appropriate // member based on the type supplied (e.g., getArray!int(), // getArray!short(), etc.) }
Aug 18 2013
On Monday, 19 August 2013 at 06:56:24 UTC, Brad Anderson wrote:import std.typetuple; struct S(T...) { alias Types = T; } alias Si = S!(int, short); alias Sf = S!(float, double); template STypes(STy) { alias STypes = STy.Types; } struct B(Ss...) { alias CombinedTypes = NoDuplicates!(TypeTuple!(staticMap!(STypes, Ss))); // I'd like to create a member here for every type in CombinedTypes // that is an array of each type. e.g., // int[] _intArray; // short[] _shortArray; // float[] _floatArray; // double[] _doubleArray; // // I don't believe the names matter because I'd probably want // to use a templated method that returns the appropriate // member based on the type supplied (e.g., getArray!int(), // getArray!short(), etc.) }For got to add a B!(Si, Sf) somewhere (I had a main() but stripped it out).
Aug 19 2013
On Monday, 19 August 2013 at 06:56:24 UTC, Brad Anderson wrote:import std.typetuple; struct S(T...) { alias Types = T; } alias Si = S!(int, short); alias Sf = S!(float, double); template STypes(STy) { alias STypes = STy.Types; } struct B(Ss...) { alias CombinedTypes = NoDuplicates!(TypeTuple!(staticMap!(STypes, Ss))); // I'd like to create a member here for every type in CombinedTypes // that is an array of each type. e.g., // int[] _intArray; // short[] _shortArray; // float[] _floatArray; // double[] _doubleArray; // // I don't believe the names matter because I'd probably want // to use a templated method that returns the appropriate // member based on the type supplied (e.g., getArray!int(), // getArray!short(), etc.) }Take a look at the implementation of std.typetuple.Tuple, it does something similar.
Aug 19 2013
On Monday, 19 August 2013 at 16:22:21 UTC, John Colvin wrote:On Monday, 19 August 2013 at 06:56:24 UTC, Brad Anderson wrote:I actually looked there first but it parses the types to support named parameters so it was much more complicated than I could use.import std.typetuple; struct S(T...) { alias Types = T; } alias Si = S!(int, short); alias Sf = S!(float, double); template STypes(STy) { alias STypes = STy.Types; } struct B(Ss...) { alias CombinedTypes = NoDuplicates!(TypeTuple!(staticMap!(STypes, Ss))); // I'd like to create a member here for every type in CombinedTypes // that is an array of each type. e.g., // int[] _intArray; // short[] _shortArray; // float[] _floatArray; // double[] _doubleArray; // // I don't believe the names matter because I'd probably want // to use a templated method that returns the appropriate // member based on the type supplied (e.g., getArray!int(), // getArray!short(), etc.) }Take a look at the implementation of std.typetuple.Tuple, it does something similar.
Aug 19 2013
On Monday, 19 August 2013 at 06:56:24 UTC, Brad Anderson wrote:...I remember asking Andrei on IRC about feature as "declaration foreach" to exactly simplify such patterns. He kind of agreed it may be useful and wanted to consult with Walter if it is worth a DIP but seems like everyone including me has forgot it :) In a meanwhile, here is a recursive template-based alternative: http://dpaste.dzfl.pl/f9def804
Aug 19 2013
On Monday, 19 August 2013 at 16:40:45 UTC, Dicebot wrote:On Monday, 19 August 2013 at 06:56:24 UTC, Brad Anderson wrote:Ok, that helps me understand how to do it with recursion and mixin templates. I ended up doing it like this: http://dpaste.dzfl.pl/14864b1b with help from Jakob Ovrum in IRC....I remember asking Andrei on IRC about feature as "declaration foreach" to exactly simplify such patterns. He kind of agreed it may be useful and wanted to consult with Walter if it is worth a DIP but seems like everyone including me has forgot it :) In a meanwhile, here is a recursive template-based alternative: http://dpaste.dzfl.pl/f9def804
Aug 19 2013
On Monday, 19 August 2013 at 17:03:18 UTC, Brad Anderson wrote:Ok, that helps me understand how to do it with recursion and mixin templates. I ended up doing it like this: http://dpaste.dzfl.pl/14864b1b with help from Jakob Ovrum in IRC.Yes, if you can stick to semantics close to built-in value tuples, it is a better solution. My snippet is a generic approach for any "generate some code for every member of the tuple" task but it does not scale good in terms of template bloat.
Aug 19 2013