A Discussion of Tuple Syntax

Meta jared771 at gmail.com
Mon Aug 19 15:04:06 PDT 2013


On Monday, 19 August 2013 at 21:03:50 UTC, Andrei Alexandrescu 
wrote:
> I'm saying that there's a mix of useful stuff and just 
> syntactic additions that are arguably less so. In my opinion:
>
> a) destructuring tuples in auto declarations - good to have:
>
> auto (a, b, c) = 
> functionReturningTupleOrStaticArrayWith3Elements();
>
> b) syntactic support for ignoring certain members in a 
> destructuring - is that really needed?
>
> auto (a, ?, c) = 
> functionReturningTupleOrStaticArrayWith3Elements();

In fairness, it is very common in other languages with pattern 
matching/destructuring. Off the top of my head I can think of 
Haskell, ML, Racket, Javascript (destructuring only) and Rust. 
This syntax is more important when pattern matching, but also 
seems to be almost universally used in destructuring. From the 
DIP:

switch (tup) {
     case {1, 2}:
     case {$, 2}: //Don't care what the first value is, only match 
on second
     case {1, x}:
     default:
}

You could just replace {$, 2} with {x, 2} and never use x, but 
this creates the problem that Bearophile mentioned.

auto t1 = #(5, "hello", 1.5); //Fine
auto #(_,  _, x) = t1; //Dammit, can't use _ twice as it's a 
variable

If you replace _ with throwaway variable names, it becomes much 
less clear (IMO) exactly what is going on. Any programmer reading 
your code would have to examine the function to see if the 
bindings introduced are ever used, or if they are just throwaways.

Maybe it's unproductive to argue over the colour of the bike 
shed, but we need to know whether it's worth adding windows.


More information about the Digitalmars-d mailing list