www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Passing Tuple Arguments to Tuple

reply Xinok <xnknet gmail.com> writes:
I was experimenting with recursive templates. I successfully created a
template which could test if a value is prime or not. What I tried to do next
is create another template which would use a tuple to build a list of primes.

template IsPrime(uint V, uint M = 2){
	static if(V == 0 || V == 1) const bool IsPrime = false;
	else static if(M*M <= V){
		static if(V%M == 0) const bool IsPrime = false;
		else const bool IsPrime = IsPrime!(V, M+1);
	}
	else const bool IsPrime = true;
}

template FindPrime(V...){
	static if(V[0] < 2){
		alias V[1..length] FindPrime;
	}
	static if(IsPrime!(V[0])){
		alias FindPrime!(V[0]-1, V[0..length]) FindPrime; // Error: 'tuple V is used
as a type'
	}
	else{
		alias FindPrime!(V[0]-1, V[1..length]) FindPrime;
	}
}


This error makes no sense to me. What's the problem? Why can't you pass tuples
to tuples?
Dec 24 2006
parent reply BCS <BCS pathilink.com> writes:
Xinok wrote:
 I was experimenting with recursive templates. I successfully created a
 template which could test if a value is prime or not. What I tried to do next
 is create another template which would use a tuple to build a list of primes.
 
 template IsPrime(uint V, uint M = 2){
 	static if(V == 0 || V == 1) const bool IsPrime = false;
 	else static if(M*M <= V){
 		static if(V%M == 0) const bool IsPrime = false;
 		else const bool IsPrime = IsPrime!(V, M+1);
 	}
 	else const bool IsPrime = true;
 }
 
 template FindPrime(V...){
 	static if(V[0] < 2){
 		alias V[1..length] FindPrime;
 	}
 	static if(IsPrime!(V[0])){
 		alias FindPrime!(V[0]-1, V[0..length]) FindPrime; // Error: 'tuple V is used
 as a type'
 	}
 	else{
 		alias FindPrime!(V[0]-1, V[1..length]) FindPrime;
 	}
 }
 
 
 This error makes no sense to me. What's the problem? Why can't you pass tuples
 to tuples?
I haven't actually played around with your code but this looks like a bug[1] I keep running into. try this static if(IsPrime!(V[0])){ const uint abc = V[0]; ///<<<<<<< alias FindPrime!(abc-1, V[0..length]) FindPrime; } it messes up implicit proposes but it often makes things work. OTOH a cast might do the trick but I haven tried that. [1] I think this is a bug (see my posts in digitalmars.d.announce about "spirit in 100 LOC") but I'm, not sure it tectonically is incorrect.
Dec 24 2006
parent reply Xinok <xnknet gmail.com> writes:
I found out it's a problem with using an index []

template a(V...){
	alias V a;
}

template b(V...){
	alias a!(V) b; // This compiles OK
	// alias a!(V[0]) b; // But this gives the error
	// alias a!(V[0..length]) b; // And so does this
}
Dec 26 2006
parent Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Xinok wrote:
 I found out it's a problem with using an index []
 
 template a(V...){
 	alias V a;
 }
 
 template b(V...){
 	alias a!(V) b; // This compiles OK
 	// alias a!(V[0]) b; // But this gives the error
 	// alias a!(V[0..length]) b; // And so does this
 }
Yes, this is a known issue. See bugs 582 and 586: http://d.puremagic.com/issues/show_bug.cgi?id=582 http://d.puremagic.com/issues/show_bug.cgi?id=586 -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Dec 26 2006