Tuple literal syntax

Michel Fortin michel.fortin at michelf.com
Thu Oct 7 09:19:00 PDT 2010


On 2010-10-07 02:04:35 -0400, Walter Bright <newshound2 at digitalmars.com> said:

> There have been a couple of looong threads about tuples:
> 
> http://www.digitalmars.com/d/archives/digitalmars/D/Reddit_why_aren_t_people_using_D_93528.html

http://www.digitalmars.com/d/archives/digitalmars/D/Should_the_comma_operator_be_removed_in_D2_101321.html

A 
> 
> lot of it foundered on what the syntax for tuple literals should be. 
> The top of the list is simply enclosing them in ( ). The problem with 
> this is
> 
>   (expression)
> 
> Is that a parenthesized expression, or a tuple? This really matters, 
> since (e)[0] means very different things for the two.

> Finally, I got to thinking, why not just make it a special case:
> 
> 
>   ( ) == tuple
>   (a) == parenthesized expression
>   (a,b) == tuple
>   (a,b,c) == tuple
>   (a,b,c,d) == tuple
> 
> etc.

Seems good. I know some people have complained about the lack of a 
semantic foundation, but my understanding is that this is simply a 
syntax to define the same kind of tuple as you get with variadic 
template arguments or variables made from types defined as variadic 
template arguments. The semantic foundation is already there and in use 
with variadic templates, it just lacks a few features (literals and the 
ability to be a return type).

If this can be used in place of both Tuple!() and TypeTuple!() defined 
in Phobos, then it'll be great as we'll no longer need to have two 
distinct tuple concepts: one in the language and another Tuple!() 
wrapper that sits top of it just so we can return a tuple from a 
function.

For the syntax, I'd like to second Juanjo's suggestion to mimic Python 
for the one-element tuple:

	(a,) == tuple

And Don's extension of that suggestion that it always accept a trailing 
comma like enums and array literals:

	(a,b,) == tuple
	(a,b,c,) == tuple


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list