Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
May 04, 2013 Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
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 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diggory | 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? http://dlang.org/phobos/std_typetuple#.staticIndexOf -- Simen |
May 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | 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?
>
> http://dlang.org/phobos/std_typetuple#.staticIndexOf
It's not a TypeTuple, it's a tuple of strings.
|
May 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diggory | 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 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diggory | 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 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 05, 2013 Re: Check if tuple contains value at compile time | ||||
---|---|---|---|---|
| ||||
Posted in reply to Diggory | 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 |
Copyright © 1999-2021 by the D Language Foundation