A different tuple syntax

bearophile bearophileHUGS at lycos.com
Thu May 30 05:29:11 PDT 2013


Regarding the syntax to unpack tuples into single variables, 
Kenji Hara wrote a DIP (http://wiki.dlang.org/DIP32 ) denoting 
tuples with the univesal syntax {...}, but people have found some 
problems in it.

(I think Kenji didn't update that DIP with all the small 
improvements we suggested in that thread, so they risk getting 
lost.)

Maybe one solution is to use a "tup(...)" syntax, it's a bit 
heavy, but it's clear and maybe it has no corner cases:

tup(int, string) tup = tup(1, "hi");
foreach (Float; tup(float, double, real)) { ... }
auto tup(x, y) = tup(1, "hi");
tup(auto x, y) = tup(1, "hi");
tup(int x, string y) = tup(1, "hi");
foreach (i, const tup(x, y); [tup(1,2), tup(3,4), tup(5,6), ...]) 
{
void foo(tup(int, string name), string msg);
(tup(A a, B b)) => a + b;
switch (tup) { case tup(1, 2): ... }


This is not very elegant, "tup" becomes a new keyword and 
generally I don't like such strong abbreviations, like the ones 
used in Rust language, but "tuple()" clashes with the 
library-defined ones, and it's even more wordy if you want to 
define an array of them:

[tuple(1,2), tuple(3,4), tuple(5,6), ...]

Bye,
bearophile


More information about the Digitalmars-d mailing list