indexing tuples using strings

BCS ao at
Mon Dec 1 14:49:24 PST 2008

Reply to Jason,

> Bill Baxter Wrote:
>> On Tue, Dec 2, 2008 at 6:40 AM, Jarrett Billingsley
>> <jarrett.billingsley at> wrote:
>>> On Mon, Dec 1, 2008 at 4:19 PM, llee <larry at>
>>> wrote:
>>>> Is is possible to index the elements within a tuple using strings?
>>>> Something similar to the way that associative arrays allow elements
>>>> to be indexed using strings.
>>> No, because that makes no sense.
>>> What does tuple["string"] mean, anyway?
>> Tuples have an annoying "auto-flattening" behavior which means that
>> it's difficult to create very advanced data structures out of them.
>> But you can create a flat tuple a-list with entries like   "fred",
>> int, "barney", double, "wilma", ireal
>> Then you can write some template functions that will scan through the
>> list for a particular string and return the following type if there's
>> a match.  You will need to use lots of recursive template-fu.
>> --bb
> ... Or you can just loop over the tuple and return the matching
> index... No recursive template-foo needed

you can't loop over a template outside of a function.


template IndexOf(char[] str, Strs...)
    const int IndexOf = typeof(
       foreach(int i, char[] s; Strs)
           static char[i] r;
           static if(Strs[i] == str)
               return &r;

import std.stdio;
void main()
    writef("%d\n", IndexOf!("hello", "go", "say", "hello", "to", "bob"));

But Gahh!!!

More information about the Digitalmars-d mailing list