www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check if tuple contains value at compile time

reply "Diggory" <diggsey googlemail.com> writes:
I'm trying to test using a "static if" statement if a tuple of 
strings contains a particular string. What's the easiest/best way 
to do this?
May 04 2013
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2013-05-05, 01:42, Diggory wrote:

 I'm trying to test using a "static if" statement if a tuple of strings  
 contains a particular string. What's the easiest/best way to do this?
-- Simen
May 04 2013
parent reply "Diggory" <diggsey googlemail.com> writes:
On Sunday, 5 May 2013 at 00:10:27 UTC, Simen Kjaeraas wrote:
 On 2013-05-05, 01:42, Diggory wrote:

 I'm trying to test using a "static if" statement if a tuple of 
 strings contains a particular string. What's the easiest/best 
 way to do this?
It's not a TypeTuple, it's a tuple of strings.
May 04 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Diggory:

 It's not a TypeTuple, it's a tuple of strings.
Then one simple way to do it is to convert it into an array of strings, and then use canFind: [mytuple[]].canFind(needle) Bye, bearophile
May 04 2013
parent reply "Diggory" <diggsey googlemail.com> writes:
On Sunday, 5 May 2013 at 00:33:34 UTC, bearophile wrote:
 Diggory:

 It's not a TypeTuple, it's a tuple of strings.
Then one simple way to do it is to convert it into an array of strings, and then use canFind: [mytuple[]].canFind(needle) Bye, bearophile
OK, that makes sense but I'm not sure I understand that syntax. The documentation seems too say that "[mytuple]" will make an array, or that "mytuple[]" will make a slice from a tuple (presumably with no arguments it will slice the entire tuple?), so how does "[mytuple[]]" work?
May 04 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Diggory:

 The documentation seems too say that "[mytuple]" will make an 
 array,
Nope. You have to extract the inherent typetuple first. And this is what the [] syntax does (tested): import std.stdio, std.typecons, std.algorithm; void main() { auto t = tuple("foo", "bar", "spam"); assert([t[]].canFind("bar")); } Bye, bearophile
May 04 2013
parent reply "Diggory" <diggsey googlemail.com> writes:
On Sunday, 5 May 2013 at 01:44:19 UTC, bearophile wrote:
 Diggory:

 The documentation seems too say that "[mytuple]" will make an 
 array,
Nope. You have to extract the inherent typetuple first. And this is what the [] syntax does (tested): import std.stdio, std.typecons, std.algorithm; void main() { auto t = tuple("foo", "bar", "spam"); assert([t[]].canFind("bar")); } Bye, bearophile
Is the behaviour of the empty [] when applied to tuples documented anywhere? The problem is that this doesn't work if the tuple is empty: Error: template std.algorithm.canFind does not match any function template declaration. And unfortunately in the situation I need it for an empty tuple is one of the most likely scenarios.
May 04 2013
parent "bearophile" <bearophileHUGS lycos.com> writes:
Diggory:

 Is the behaviour of the empty [] when applied to tuples 
 documented anywhere?
I don't remember.
 The problem is that this doesn't work if the tuple is empty:
 Error: template std.algorithm.canFind does not match any 
 function template declaration.

 And unfortunately in the situation I need it for an empty tuple 
 is one of the most likely scenarios.
I see. Then a good idea is to create a little function, to solve this. It should contain a static if that tests for the empty tuple and returns false in that case, and otherwise uses the canFind. Bye, bearophile
May 05 2013