<div dir="ltr">2013/3/29 bearophile <span dir="ltr"><<a href="mailto:bearophileHUGS@lycos.com" target="_blank">bearophileHUGS@lycos.com</a>></span><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
One handy tuple syntax in Haskell allows you to name both the items of a tuple and it whole:<br>
<br>void foo(t2@{int a, string b}) {<br>
   // here a and b are tuple items and t2 is the whole tuple.<br>
}<br>
auto t1@{x, y} = {10, "hi"};<br>
foo(t1);<br></blockquote><div><br></div><div style>It will introduce a kind of "reference variable" to D.</div><div style><br></div><div style>auto t1@{x, y} = {10, "hi"};<br></div><div style>assert(&t1[0] == &x);</div>
<div style>// t1[0] and x may refer same address on stack.</div><div><br></div><div style>Such case, you can repeat pack and unpack.</div><div> </div>void foo({int a, string b}) {</div><div class="gmail_quote">   // auto t2 = {a, b};  // make tuple by copy, or</div>
<div class="gmail_quote">   // {a, b} = {1, 2};    // make pack and use it immediately<br>}<br><div>auto {x, y} = {10, "hi"};  foo({x, y});</div><div>  </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

auto tup = {};  // zero-element tuple (Syntax meaning will be changed!)<br>
<br>Nullary tuples are not that useful in D. Scala doesn't even have a short literal for them.<br>
<br>
So a longer syntax like this is acceptable:<br>
<br>
auto tup = Tuple();<br></blockquote><div><br></div><div style>This is for the consistency of language elements.</div><div style>If you want to zero-parameter lambda, you can write like follows.</div><div style><br></div>
<div style>auto fn = (){};<br></div><div style>auto fn = {;};<br></div><div style> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<br>
<br>
This is nice, so we are merging tuple types with tuples, this will simplify D language:<br>
<br>
// declare tuple value by using explicit tuple type<br>
{int, string} tup = {1, "hi"};<br>
<br>
 alias TL = {int, string[], double[string]};  // types<br>
<br>
<br>
But one thing to remember in the document is that here T1 and T2 are different, because your tuples do not auto-flatten as TypeTuples currently do:<br>
<br>
alias T1 = {float, double, real};<br>
alias T2 = {float, double, {real}};<br></blockquote><div><br></div><div style>It would need more description. I'll explain about that. </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<br>
<br>
foreach (Float; {float, double, real}) { ... }<br>
<br>
I think you meant to put a variable name there.<br></blockquote><div><br></div><div style>Float is the iterated type.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<br>
<br>
    {1}         // one-element tuple<br>
<br>
I presume this too will be accepted as 1-tuple:<br>
<br>
    {1,}<br></blockquote><div><br></div><div style>Currently D allows redundant commas in some places.</div><div style>void foo(int x, int y,) {}</div><div style>enum E { a = 1, b = 2, }</div><div style>auto arr = [1,2,3,];<br>
</div><div style><br></div><div style>So, compiler would accept following tuples.</div><div style><br></div><div style>auto tup = {1,};</div><div style>auto tup = {1,"hi",};<br></div><div style> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<br>
<br>
{c, $} = tup;   // Rewritten as: c = tup[0];<br>
<br>
$ is used for array lengths, so it's not so good to overload it to mean "don't care" too.<br>
<br>Alternative syntaxes:<br>
<br>
{c, $_} = tup;<br>
{c, @} = tup;<br>
{c, @_} = tup;<br>
{c, $$} = tup;<br>
{c, {}} = tup;<br>
{c, {_}} = tup;<br>
{c, $~} = tup;<br>
{c, @~= tup;<br>
etc.<br></blockquote><div><br></div><div style>Placeholder token is debatable.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<div class="im"><br>
<br>
if (auto {1, y} = tup) {<br>
    // If the first element of tup (tup[0]) is equal to 1,<br>
    // y captures the second element of tup (tup[1]).<br>
}<br>
<br>
<br></div>
I suggest to leave that pattern matching plus conditional to a future refinement of tuple implementation (a second stage. And remove it from this first stage proposal. So I suggest to split your proposal in two successive proposals). It seems handy, but D programmers need some time to go there.<br>
</blockquote><div><br></div><div style>For complex tuple unpacking requires the part of pattern matching.</div><div style><br></div><div style>auto tup = {1, {2,3,4,5,6}}<br></div><div><div>auto {x, {$, y, ...}} = tup;   // makes nested tuple pattern for unpacking</div>
<div>assert(x == 1);</div><div>assert(y == 3);</div><div><br></div></div><div style>So I'd like to keep one DIP.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

- - - - - - - - - - - -<br>
<br>
switch (tup) {<br>
    case {1, 2}:<br>
    case {$, 2}:<br>
    case {1, x}:    // capture tup[1] into 'x' when tup[0] == 1<br>
    default:        // same as {...}<br>
}<br>
<br>
<br>
What's quite important here is the "final switch". D has to make sure you are considering all possible cases.<br>
<br>
- - - - - - - - - - - -<br>
<br>
I suggest to leave this to the second stage, and remove it from this proposal:<br>
<br>
auto tup = {1, "hi", 3.14, [1,2,3]};<br>
if (auto {1, "hi", ...} = tup) {}<br>
<br>
- - - - - - - - - - - -<br>
<br>
"will" is written badly:<div class="im"><br>
<br>
// If the first element of coord is equal to 1 (== x), 'then' statement wil be evaluated.<br></div></blockquote><div><br></div><div style>Will fix.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class="im"></div>
- - - - - - - - - - - -<br>
<br>
I think this is the third thing to leave to the second stage:<div class="im"><br>int x = 1;<br>
if (auto {$x, y} = coord) { ... }<br>
<br></div>
- - - - - - - - - - - -<br>
<br>
This is nice:<br>
<br>
if (auto {x, y} = coord[]) {}   // same, explicitly expands fields<br>
<br>
- - - - - - - - - - - -<br>
<br>
This is handy and it's vaguely present in Python3, but I suggest to leave this (4th thing) to the second stage:<br>
<br>
if (auto {num, msg, ...} = tup) {}      // ok, `...` matches to zero-elements.<br></blockquote><div><br></div><div style>Kenji Hara </div></div></div></div>