<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">The `if case` is the same meaning as `switch-case`, so I don&#39;t think there is anything ambitious. For `switch-case`, it is not equal, it is matching.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Nov 9, 2016 at 7:17 PM, Nicholas Outram via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="margin:0px;line-height:normal"><font face="Arial" size="4">Hi</font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><br></font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4">I’ve been drilling down on the syntax of enumerated types with associated data in the current release version of Swift 3.</font></div><div style="margin:0px;line-height:normal"><span style="font-family:Arial;font-size:large">I’ve pasted below a section of a Playground that captures an issue I’d like to raise.</span></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><br></font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4">In summary:</font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><br></font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4">Consider the following </font></div><div style="margin:0px;line-height:normal"><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">enum</span><span style="font-variant-ligatures:no-common-ligatures"> Vehicle {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> car(petrol: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures">, sizeCC: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> plane(engines : </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> other(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> none</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">let</span><span style="font-variant-ligatures:no-common-ligatures"> myJourney : </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(83,154,164)">Vehicle</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">other</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(200,27,19)">&quot;pogo stick&quot;</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><br></font></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><i>Whereas the following is clear</i></font></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">other</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">_</span><span style="font-variant-ligatures:no-common-ligatures">) = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(83,154,164)">myJourney</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;line-height:normal"><font face="Arial" size="4"><i>the following shorthand equivalent is potentially confusing for the sake of 3 characters</i></font></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(196,34,117)">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">other</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(83,154,164)">myJourney</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;line-height:normal"><font face="Arial"><font size="4">- In the first case, the presence of the underscore does communicate that something is being assigned, but dropped.</font></font></div><div style="margin:0px;font-size:18px;line-height:normal"><font face="Arial">- In the second case, the reader could easily be mislead into thinking that = was supposed to be == as there no apparent place to assign anything.</font></div><div style="margin:0px;font-size:18px;line-height:normal"><font face="Arial"><br></font></div><div style="margin:0px;line-height:normal"><font face="Arial"><font size="4">My suggestion would simply be to drop the shorthand as it’s ambiguous?</font></font></div><div style="margin:0px;line-height:normal"><font face="Arial"><font size="4"><br></font></font></div><div style="margin:0px;line-height:normal"><font face="Arial"><font size="4"><br></font></font></div><div style="margin:0px;line-height:normal"><font face="Arial"><font size="4">Nick Outram</font></font></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><br></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275"></span></div></div></div></div><br><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="word-wrap:break-word"><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275"></span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275"><br></span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">import</span><span style="font-variant-ligatures:no-common-ligatures"> Foundation</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;min-height:21px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: Consider the following enumerated type with associated data</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">enum</span><span style="font-variant-ligatures:no-common-ligatures"> Vehicle {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures"> car(petrol: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Bool</span><span style="font-variant-ligatures:no-common-ligatures">, sizeCC: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures"> plane(engines : </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Int</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures"> other(</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">String</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures"> none</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;min-height:21px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: Let&#39;s pick an example</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">let</span><span style="font-variant-ligatures:no-common-ligatures"> myJourney : </span><span style="font-variant-ligatures:no-common-ligatures;color:#539aa4">Vehicle</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">other</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#c81b13">&quot;pogo stick&quot;</span><span style="font-variant-ligatures:no-common-ligatures">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;min-height:21px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: I now want to test what case `myJourney` is.</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//:</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: We cannot use the `==` operator because `Vehicle` has associated data. Instead we use `if case` and *simply drop the associated value* with `_` as shown above</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">other</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">_</span><span style="font-variant-ligatures:no-common-ligatures">) = </span><span style="font-variant-ligatures:no-common-ligatures;color:#539aa4">myJourney</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(200,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Somewhere nice?&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">} </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">else</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(200,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Ok, it&#39;s a secret?&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//:The above is clear enough once you get used to the syntax. The `_` communicates that a value has been dropped.</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//:</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: **However**, Swift 3 allows us to drop the parenthesis altogether and use the following shorthand:</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(83,154,164)"><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">if</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">case</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> .</span><span style="font-variant-ligatures:no-common-ligatures;color:#31595d">other</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> = </span><span style="font-variant-ligatures:no-common-ligatures">myJourney</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(200,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Somewhere nice?&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">} </span><span style="font-variant-ligatures:no-common-ligatures;color:#c42275">else</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(200,27,19)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000">   </span><span style="font-variant-ligatures:no-common-ligatures;color:#3e1e81">print</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">(</span><span style="font-variant-ligatures:no-common-ligatures">&quot;Ok, it&#39;s a secret?&quot;</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000">)</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: *Unlike the previous example, I do wonder if this is a language feature that needs review?*</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//:</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: - On face value, reading this code as is there is an assignment operator `=` with nothing apparently being assigned.</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures">//: - It also reads as if `=` should be `==`</span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:18px;line-height:normal;font-family:Menlo;color:rgb(33,150,28)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><br><br>
<br></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>