A Discussion of Tuple Syntax

Meta jared771 at gmail.com
Sat Aug 17 00:02:47 PDT 2013


On Saturday, 17 August 2013 at 05:48:47 UTC, captaindet wrote:
> not sure what we are talking about here, new syntax for
> a) TypeTuple (that would be my best guess but i am not sure)
> b) std.typecons.Tuple
> c) both, a unified syntax
> the DIP is not completely clear about this either.

First and foremost, a new syntax for std.typecons.Tuple. Creating 
tuple literals -> #(1, "a", false), pattern matching on tuples -> 
switch (#(1, 2)), and destructuring of tuples -> #(a, b) = #(1, 
2).

A good question is whether this syntax can (and more importantly, 
should) be used for TypeTuples as well. TypeTuples are poorly 
named and not at all similar to Tuple, so it doesn't make much 
sense to give them both the same syntax. Also, I can't see 
TypeTuple seeing nearly as much use as Tuple, so it may be better 
to introduce the new syntax for Tuple, and keep TypeTuple as-is.

> only case (a) is hot at the moment and i fell into some related 
> pits recently myself. so i'd love to have a better syntax, one 
> that makes the dirty hack (status quo) a proper part of the 
> language - as it is widely used already and very powerful.

What TypeTuple gives you access to is the compiler tuple type 
that it uses for function argument lists and other things. I 
wouldn't really call it a dirty hack. The two most confusing 
things about TypeTuple, I think, is the auto-expansion, and the 
fact that it can contain both types and values.

> i am curious too if ..(..) constructs are completely out of the 
> question. if not, i would suggest as shorthand:
>
> 	!(a,b)
>
> this reminds of template instantiation and is exactly how the 
> awkwardly/confusingly named TypeTuple is realized now, as an 
> alias to its own template arguments.

Unfortunately, that won't work, as the value of this expression 
is already well-defined in the current language. A list of 
comma-separated expressions means that each is evaluated from 
left to right, and then the result of the entire expression is 
the result of the rightmost expression, e.g.:

     auto a = true;
     auto b = false;
     //Evaluate a, then b. The result of the expression
     //in the brackets is b. Then the ! gets applied to b
     auto x = !(a, b);
     assert(x == true); //Passes

> if we need a name for it, it should not remind at all of 
> 'tuple' - since there is also the completely unrelated 
> std.typecons.Tuple. a more verbose/explicit suggestion would be
>
> 	symbols(a,b)

I 100% agree that TypeTuple needs a better name. A couple other 
suggestions were put forward as well in the other tuple thread 
currently active.



More information about the Digitalmars-d mailing list