www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generating members from parameter tuple

reply "Brad Anderson" <eco gnuk.net> writes:
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
next sibling parent "Brad Anderson" <eco gnuk.net> writes:
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
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
parent "Brad Anderson" <eco gnuk.net> writes:
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.
Aug 19 2013
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
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
parent reply "Brad Anderson" <eco gnuk.net> writes:
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.
Aug 19 2013
parent "Dicebot" <public dicebot.lv> writes:
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