www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template explosion

reply "JS" <js.mdnq gmail.com> writes:
It seems that one must use two templates to process built in 
times and strings

template A(string a) { ... }
template A(a) { enum A = A(typeof(a).stringof); }

This is so we can do stuff like A!(double) and A!("double").

The problem is when we have many parameters the number of 
permutations increases exponentially.

Is there a better way to unify the two?
Jul 16 2013
next sibling parent Timothee Cour <thelastmammoth gmail.com> writes:
On Tue, Jul 16, 2013 at 7:52 PM, JS <js.mdnq gmail.com> wrote:

 It seems that one must use two templates to process built in times and
 strings

 template A(string a) { ... }
 template A(a) { enum A = A(typeof(a).stringof); }

 This is so we can do stuff like A!(double) and A!("double").

 The problem is when we have many parameters the number of permutations
 increases exponentially.

 Is there a better way to unify the two?
template a(T...)if(T.length==1){ enum a=1; } void main(){ auto a1=a!double; auto a2=a!"double"; } However: This syntax sucks. Why not support the following syntax: template a(auto T) {...} with same semantics? Because this is problematic with more arguments: I'd like this: template a(auto T1, double T2){...} but instead have to do that: template a(T...)if(is(T[1]==double)) {...} and it gets quickly more complicated
Jul 16 2013
prev sibling parent reply Timothee Cour <thelastmammoth gmail.com> writes:
On Tue, Jul 16, 2013 at 8:29 PM, Timothee Cour <thelastmammoth gmail.com>wrote:

 On Tue, Jul 16, 2013 at 7:52 PM, JS <js.mdnq gmail.com> wrote:

 It seems that one must use two templates to process built in times and
 strings

 template A(string a) { ... }
 template A(a) { enum A = A(typeof(a).stringof); }

 This is so we can do stuff like A!(double) and A!("double").

 The problem is when we have many parameters the number of permutations
 increases exponentially.

 Is there a better way to unify the two?
template a(T...)if(T.length==1){ enum a=1; } void main(){ auto a1=a!double; auto a2=a!"double"; } However: This syntax sucks. Why not support the following syntax: template a(auto T) {...} with same semantics? Because this is problematic with more arguments: I'd like this: template a(auto T1, double T2){...} but instead have to do that: template a(T...)if(is(T[1]==double)) {...} and it gets quickly more complicated
A) actually, I'm not sure if auto would be the best keyword here, but maybe another keyword. B) What's the rationale why alias can't match to a primitive type in the first place? C) correction: above, it should be: template a(T...)if(T.length==2 && is(T[1]==double)) {...} which is quite verbose.
Jul 16 2013
parent "JS" <js.mdnq gmail.com> writes:
On Wednesday, 17 July 2013 at 03:34:17 UTC, Timothee Cour wrote:
 On Tue, Jul 16, 2013 at 8:29 PM, Timothee Cour 
 <thelastmammoth gmail.com>wrote:

 On Tue, Jul 16, 2013 at 7:52 PM, JS <js.mdnq gmail.com> wrote:

 It seems that one must use two templates to process built in 
 times and
 strings

 template A(string a) { ... }
 template A(a) { enum A = A(typeof(a).stringof); }

 This is so we can do stuff like A!(double) and A!("double").

 The problem is when we have many parameters the number of 
 permutations
 increases exponentially.

 Is there a better way to unify the two?
template a(T...)if(T.length==1){ enum a=1; } void main(){ auto a1=a!double; auto a2=a!"double"; } However: This syntax sucks. Why not support the following syntax: template a(auto T) {...} with same semantics? Because this is problematic with more arguments: I'd like this: template a(auto T1, double T2){...} but instead have to do that: template a(T...)if(is(T[1]==double)) {...} and it gets quickly more complicated
A) actually, I'm not sure if auto would be the best keyword here, but maybe another keyword. B) What's the rationale why alias can't match to a primitive type in the first place?
I think this is the real problem, else it would be rather easy. I don't mind writing static if's for each argument as that's linear.
 C)
 correction: above, it should be:
 template a(T...)if(T.length==2 && is(T[1]==double)) {...}
 which is quite verbose.
Jul 16 2013