Thread overview
Generating members from parameter tuple
Aug 19, 2013
Brad Anderson
Aug 19, 2013
Brad Anderson
Aug 19, 2013
John Colvin
Aug 19, 2013
Brad Anderson
Aug 19, 2013
Dicebot
Aug 19, 2013
Brad Anderson
Aug 19, 2013
Dicebot
August 19, 2013
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.)
}
August 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.)
> }

For got to add a B!(Si, Sf) somewhere (I had a main() but stripped it out).
August 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.
August 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
August 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:
>> 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.

I actually looked there first but it parses the types to support named parameters so it was much more complicated than I could use.
August 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:
>> ...
>
> 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

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.
August 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.