Tuple literal syntax
Denis Koroskin
2korden at gmail.com
Thu Oct 7 00:55:07 PDT 2010
On Thu, 07 Oct 2010 10:04:35 +0400, Walter Bright
<newshound2 at digitalmars.com> wrote:
> 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.
>
> No ambiguities! Only one special case. I submit this special case is
> rare, because who wants to define a function that returns a tuple of 1?
> Such will come about from generative programming, but:
>
> (a,b,c)[0]
>
> may be how the generative programming works, and that suggests:
>
> (a,0)[0]
>
> as how a user could generate a tuple of 1. Awkward, sure, but like I
> said, I think this would be rare.
If tuples become first-class citizens of D lands, is it possible to make
'void' and alias to an empty tuple? I believe they are basically the same
thing. A function that accepts no arguments may be defined as a function
that accepts a tuple of size 0:
void f1(string, string);
auto tuple1 = ("hello, %s", "world");
f1(tuple1); // works
void f2();
auto tuple2 = ();
f2(tuple2); // should work, too
That would allow creation a variables of type void, which is very useful
for generic programming. Here is an example:
auto proxy(alias f)()
{
auto result = f();
do(stuff);
return result;
}
Works for any functions 'f' but returning voids.
I know it have been asked many times with no success, but still...
More information about the Digitalmars-d
mailing list