Tuples

Jonathan M Davis jmdavisProg at gmx.com
Sun May 26 18:36:44 PDT 2013


On Monday, May 27, 2013 11:21:08 Manu wrote:
> I have certainly been confused by the term 'tuple' used in D countless
> times.
> It seems to exist in a variety of different contexts, and every time I
> think I understood it, I realise I'm probably wrong some time later.
> There seems to be poor/no documentation comparing/distinguishing various D
> tuples, also, there seems to be no real explicit syntax, which makes it
> hard to recognise.
> I can kinda handle a Tuple template, and then there are these type tuples,
> and then expression tuples? What is the relationship between these? Are
> they compatible? How do I distinguish them?

You basically just have to worry about Tuple and TypeTuple. Tuple is of course 
very easy to understand. TypeTuple is then just an alias for the built-in 
tuples, which can hold basically anything. They're what template parameter 
lists, function parameter lists, template argument lists, and function 
argument lists are made of. And you can use them handily in other places (such 
as foreach) by using TypeTuple explicitly. I believe that an expression tuple 
is simply a TypeTuple which holds only expressions. In practice, I think that 
the differences are fairly simple, but they take quite a bit to wrap your head 
around - primarily due to the terminology used rather than the actual concept.

> Someone that understand what they're talking about should take some time to
> write a comprehensive article on the matter :)

I guess that I'll have to look at doing that after I finish the article that 
I'm writing on ranges (that I really need to finish...).

> Some of the things I have read seem to presume that I already know what
> it's talking about, and as a result, lead to dismissing or misunderstanding
> the article.

I generally know what's going on with D, but there were talks at dconf that 
taught be stuff about D that I'd either forgotten or never known (like some of 
what was going on with moves in Ali's talk). D is nowhere near as bad C++ with 
regards to how complicated it is, but it's still quite complicated.

> I think the practical take-away from my experience is that it ends up as
> one of those 'too hard' concepts, that I develop a tendency to actively
> avoid dealing with because it's confused me a number of times.
> I typically just start typing stuff and hope it works. And if it doesn't I
> fiddle with it until it eventually does (or I give up), and I never
> *really* understand why.
> I'll bet I'm not the only one...

It's well worth understanding TypeTuple well enough to use it with foreach, as 
it's a great way to generate unit tests, particularly when you're dealing with 
templated functions. Phobos does that quite a lot, particularly with strings. 
Take splitLines unit test for example

    foreach (S; TypeTuple!(char[], wchar[], dchar[], string, wstring, 
dstring))
    {
        auto s = 
to!S("\rpeter\n\rpaul\r\njerry\u2028ice\u2029cream\n\nsunday\n");

        auto lines = splitLines(s);
        assert(lines.length == 9);
        assert(lines[0] == "");
        assert(lines[1] == "peter");
        assert(lines[2] == "");
        assert(lines[3] == "paul");
        assert(lines[4] == "jerry");
        assert(lines[5] == "ice");
        assert(lines[6] == "cream");
        assert(lines[7] == "");
        assert(lines[8] == "sunday");

        lines = splitLines(s, KeepTerminator.yes);
        assert(lines.length == 9);
        assert(lines[0] == "\r");
        assert(lines[1] == "peter\n");
        assert(lines[2] == "\r");
        assert(lines[3] == "paul\r\n");
        assert(lines[4] == "jerry\u2028");
        assert(lines[5] == "ice\u2029");
        assert(lines[6] == "cream\n");
        assert(lines[7] == "\n");
        assert(lines[8] == "sunday\n");

        s.popBack(); // Lop-off trailing \n
        lines = splitLines(s);
        assert(lines.length == 9);
        assert(lines[8] == "sunday");

        lines = splitLines(s, KeepTerminator.yes);
        assert(lines.length == 9);
        assert(lines[8] == "sunday");
    }

You get to unit test with 6 different types while only writing the code once. 
That can be _extremely_ useful.

- Jonathan M Davis


More information about the Digitalmars-d mailing list