<div dir="ltr">On Mon, May 8, 2017 at 2:51 AM, André Videla <span dir="ltr"><<a href="mailto:andre.videla@gmail.com" target="_blank">andre.videla@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra">Let me show you:</div><div class="gmail_extra">
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">assume we have this data type which is just a pair of Ints</span></p><span class=""><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">enum</span><span class="m_963695162173979960gmail-s2"> Pair {</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> point(x: </span><span class="m_963695162173979960gmail-s3">Int</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s3">Int</span><span class="m_963695162173979960gmail-s2">)</span></p><p class="m_963695162173979960gmail-p1">
</p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p></span><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">and see how Swift allows us to deconstruct it:</span></p></div><div class="gmail_extra">
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> .</span><span class="m_963695162173979960gmail-s3">point</span><span class="m_963695162173979960gmail-s2">(let x, let y) = </span><span class="m_963695162173979960gmail-s3">Pair</span><span class="m_963695162173979960gmail-s2">.</span><span class="m_963695162173979960gmail-s3">point</span><span class="m_963695162173979960gmail-s2">(x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s5">print</span><span class="m_963695162173979960gmail-s2">(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">this is perfectly fine. Even if the labels are omitted the structure is kept and x = 3 and y = 5</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> .</span><span class="m_963695162173979960gmail-s3">point</span><span class="m_963695162173979960gmail-s2">(x: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> x, y: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> y) = </span><span class="m_963695162173979960gmail-s3">Pair</span><span class="m_963695162173979960gmail-s2">.</span><span class="m_963695162173979960gmail-s3">point</span><span class="m_963695162173979960gmail-s2">(x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s5">print</span><span class="m_963695162173979960gmail-s2">(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">perfectly fine, x and y are given and correspond to the actual label of the enum case x = 3, y = 5</span></p>
<p class="m_963695162173979960gmail-p2"><span class="m_963695162173979960gmail-s2"></span><br></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> .point(y: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> x, x: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> y) = </span><span class="m_963695162173979960gmail-s3">Pair</span><span class="m_963695162173979960gmail-s2">.</span><span class="m_963695162173979960gmail-s3">point</span><span class="m_963695162173979960gmail-s2">(x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> print(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">This is an error, as expected, labels do not correspond to any existing known structure and the match makes no sense. It does not compile</span></p>
<p class="m_963695162173979960gmail-p1">Now we refactor the code a bit and we start using a pair instead of an enum case</p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> (let x, let y) = (x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s5">print</span><span class="m_963695162173979960gmail-s2">(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">this is fine since the structure is preserved from the value on the right, x = 3, y = 5</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> (x: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> x, y: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> y) = (x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s5">print</span><span class="m_963695162173979960gmail-s2">(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">this is fine since the labels correspond to the existing structure on the right x = 3, y = 5</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s1">if</span><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s1">case</span><span class="m_963695162173979960gmail-s2"> (y: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> x, x: </span><span class="m_963695162173979960gmail-s1">let</span><span class="m_963695162173979960gmail-s2"> y) = (x: </span><span class="m_963695162173979960gmail-s4">3</span><span class="m_963695162173979960gmail-s2">, y: </span><span class="m_963695162173979960gmail-s4">5</span><span class="m_963695162173979960gmail-s2">) {</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2"> </span><span class="m_963695162173979960gmail-s5">print</span><span class="m_963695162173979960gmail-s2">(</span><span class="m_963695162173979960gmail-s4">"</span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">x</span><span class="m_963695162173979960gmail-s4">), </span><span class="m_963695162173979960gmail-s2">\</span><span class="m_963695162173979960gmail-s4">(</span><span class="m_963695162173979960gmail-s2">y</span><span class="m_963695162173979960gmail-s4">)"</span><span class="m_963695162173979960gmail-s2">)</span></p>
<p class="m_963695162173979960gmail-p1"><span class="m_963695162173979960gmail-s2">}</span></p><p class="m_963695162173979960gmail-p1">And this.</p><p class="m_963695162173979960gmail-p1">This compiles even though the <b>structure matched does not correspond to the structure of the value against which it matches</b>. x = 5, y = 3</p></div></div>
</blockquote></div>Yes, and Robert is proposing to eliminate tuple shuffles from the language; if accepted, then this last example will behave differently.</div></div>