digitalmars.D.learn - Check if tuple contains value at compile time
- Diggory (3/3) May 04 2013 I'm trying to test using a "static if" statement if a tuple of
- Simen Kjaeraas (4/6) May 04 2013 http://dlang.org/phobos/std_typetuple#.staticIndexOf
- Diggory (2/7) May 04 2013 It's not a TypeTuple, it's a tuple of strings.
- bearophile (6/7) May 04 2013 Then one simple way to do it is to convert it into an array of
- Diggory (6/13) May 04 2013 OK, that makes sense but I'm not sure I understand that syntax.
- bearophile (10/12) May 04 2013 Nope. You have to extract the inherent typetuple first. And this
- Diggory (8/20) May 04 2013 Is the behaviour of the empty [] when applied to tuples
- bearophile (8/15) May 05 2013 I see. Then a good idea is to create a little function, to solve
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
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
On Sunday, 5 May 2013 at 00:10:27 UTC, Simen Kjaeraas wrote:On 2013-05-05, 01:42, Diggory wrote:It's not a TypeTuple, it's a tuple of strings.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
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
On Sunday, 5 May 2013 at 00:33:34 UTC, bearophile wrote:Diggory: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?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
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
On Sunday, 5 May 2013 at 01:44:19 UTC, bearophile wrote:Diggory: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.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
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