Thread overview
Passing Tuple Arguments to Tuple
Dec 25, 2006
Xinok
Dec 25, 2006
BCS
Dec 26, 2006
Xinok
Dec 26, 2006
Kirk McDonald
December 25, 2006
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?
December 25, 2006
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.
December 26, 2006
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
}
December 26, 2006
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