<div dir="ltr"><div>I&#39;d like some feedback from the core team to see if eliminating the ternary operator and replacing it with if-else is even proposal worthy. </div><div><br></div><div>Some interesting code in the standard library that uses the ternary operator quite a bit. </div><div><br></div><a href="https://gist.github.com/masters3d/7b0c6f3653368afe4357">https://gist.github.com/masters3d/7b0c6f3653368afe4357</a><br><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 4, 2015 at 12:50 PM, J. Cheyo Jimenez <span dir="ltr">&lt;<a href="mailto:cheyo@masters3d.com" target="_blank">cheyo@masters3d.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The motive is to desist the use of the question mark outside of the swift optional context.<div>I like the rust style if-expression as an alternative. <br><div>Imo consistency and readability is a little more important than brevity.</div><div><div class="h5"><div><font size="2"><span style="background-color:rgba(255,255,255,0)"></span><span><br></span></font><br>On Friday, December 4, 2015, David Hart &lt;<a href="mailto:david@hartbit.com" target="_blank">david@hartbit.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>Best proposition since, but still more verbose than the ternary operator.</div><div><br>On 04 Dec 2015, at 18:27, David Owens II &lt;<a>david@owensd.io</a>&gt; wrote:<br><br></div><blockquote type="cite"><div>There’s no need for the “then&quot; keyword if you just allow if-expressions as well.<div><br></div><div>```</div><div>let result = if condition { 1 } else { 2 }</div><div>```</div><div><br></div><div>Or more verbosely:</div><div><br></div><div><div>```</div><div>let result =</div><div>    if condition {</div><div>        return 1</div><div>    }</div><div>    else {</div><div>        return 2</div><div>    }</div><div>```</div></div><div><br></div><div>The same could be for switch statements as well.</div><div><br></div><div>```</div><div>let result = switch item {</div><div>    case foo: 1</div><div>    case bar: 2</div><div>}</div><div>```</div><div><br></div><div>The above assume implicit returns on single-line statements on the condition/case branches.</div><div><br></div><div>-David</div><div><br><div><blockquote type="cite"><div>On Dec 3, 2015, at 11:26 PM, David Hart &lt;<a>david@hartbit.com</a>&gt; wrote:</div><br><div><div dir="auto"><div>I also tend to agree but I don&#39;t like the ordering in the Python version and would rather see the introduction of the then keyword<br></div><div><br>On 04 Dec 2015, at 06:08, Sean Heber &lt;<a>sean@fifthace.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div>I agree that using ? for ternary feels.. uncomfortable in Swift. I wouldn&#39;t mind seeing Python-style or just eliminating it entirely in favor of simplicity.</div><div><br></div><div>l8r</div><div>Sean<br></div><div><br>On Dec 3, 2015, at 10:48 PM, Dan Appel &lt;<a>dan.appel00@gmail.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><span><div style="font-family:Helvetica,Arial;margin:0px">I agree that ternary can make code unreadable, especially so with the ‘?’. I personally like the Haskel option the most, but it introduces a new keyword ‘then’, which isn’t great either. Python’s &quot;y if x else z&quot; is therefore, in my opinion, the best alternative.</div><div style="font-family:Helvetica,Arial;margin:0px"><br></div><div style="font-family:Helvetica,Arial;margin:0px">Also, I found it interesting how split the Rust community was about removing the ternary operator.</div><div style="font-family:Helvetica,Arial;margin:0px"><br></div><div style="font-family:Helvetica,Arial;margin:0px"><span style="font-family:helvetica,arial">— </span></div><div style="font-family:Helvetica,Arial"><div style="font-family:helvetica,arial">Dan Appel</div></div></span></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Dec 3, 2015 at 7:58 PM J. Cheyo Jimenez &lt;<a>cheyo@masters3d.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><span style="font-size:12.8px">Would you consider replacing the C style ?: ternary operator to something that does not use the question mark and colon ? </span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">The use of &quot;?&quot; can be confusing when speed reading code with optionals. </div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Consider this code as somebody learning swift as their first language.</div><div style="font-size:12.8px"><p><font face="monospace, monospace">let result = !condition ? 1 : 2</font></p></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Some alternatives:</div><div style="font-size:12.8px"><div><br></div><div>Haskel <div><div dir="ltr" style="direction:ltr;color:rgb(37,37,37);font-family:sans-serif;font-size:14px;line-height:14.9333px;background:rgb(248,248,248)"><pre style="white-space:pre-wrap;font-family:monospace,Courier;border:1px solid rgb(221,221,221);padding:1em;line-height:1.3em;background-color:rgb(249,249,249)"><span style="color:rgb(0,128,0);font-weight:bold">if</span> predicate <span style="color:rgb(0,128,0);font-weight:bold">then</span> expr1 <span style="color:rgb(0,128,0);font-weight:bold">else</span> expr2</pre></div><div>Python</div></div><div><pre style="white-space:pre-wrap;font-family:monospace,Courier;border:1px solid rgb(221,221,221);padding:1em;line-height:1.3em;font-size:14px;background-color:rgb(249,249,249)">result <span style="color:rgb(102,102,102)">=</span> x <span style="color:rgb(0,128,0);font-weight:bold">if</span> a <span style="color:rgb(102,102,102)">&gt;</span> b <span style="color:rgb(0,128,0);font-weight:bold">else</span> y</pre></div><div>The advantage of not using the question mark is that the language will be more consistency on the use of &quot;?&quot; to mean only optionals. <br></div><div><br></div><div><br></div><div>References:</div><div><a href="https://devforums.apple.com/message/1020139#1020139" target="_blank">https://devforums.apple.com/message/1020139#1020139</a></div></div></div><div style="font-size:12.8px"><a href="https://github.com/rust-lang/rust/issues/1698#issuecomment-3705066" target="_blank">https://github.com/rust-lang/rust/issues/1698#issuecomment-3705066</a></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=4Q3dO0ELn940F2HNV6NWP3n6-2BFmLgXvEeytAtbr-2BgeDYoD5tpBTS72h2nPdoesBjFIHECaHIxYh10FYCXUeWQ65fPIrQZyTNAdleeXCQ64naCq-2BRo4e2zXf0J9A2HRmL88OFXVfBZ5quTXBpurvrqBBs4d-2ByKIvlmbh88TYRH4UfeDBg9-2Fm70jQ1K-2FTBCibgE3e1ZTsN44r9UpWEyJfJ-2F3E-2Fhz7mL2i3piK7DxnoOdI-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
_______________________________________________<br>
swift-evolution mailing list<br>
<a>swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=CzM39owGTpkJ1zyGXnrRVEXja65D5KHvjV3-2BNib8-2FALErD0kiUUIMIXRwdeL5Lu76KgH2bdx5yKs2bSajEksH7zgA3iXmq0rXx62UiivBwrhmvKJ2lmUUvw-2F1goy53ob9w0N-2B043Vqbsv4aPVvPG12sD1tuiB1iZXAJIXUr6PdM3alK-2Fg1QSW0tRJlIaq-2BL1Q9kmi4L-2BquSs5eiGROE3kA45gCSlnV2ljaEzqH8fWPY-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a>swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43hXwizS3O2z60WweqomIrdhDQaJFFM-2BMbJRHzRQnffAg0Zt6UaycrX12nXLIZFiAgcLjU96ZfPh5dRJBCSLNILhlCY95BzqlLpT6pX-2B9q7BavrJVjkYQtQI38LVaaIEPfHKkmPYmu-2BtgwQR1qa66zWTe-2F7Q0MsLc7ADrUiJGrsP6zzDHna2ucVlAZJoSk2u7ptKO4FK165xJajrxeQxQvsc-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">

</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a>swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nE9rxSXA5G4kxsTVkgv43pXkLx-2B36P-2BPNJufHeY0dgdbCp-2BWhOxq3PDIqAyBYGQ0kWPIv4qdll8LAWoMYQohnn8bX9mxOk66coEw-2BVuBTnfhBCE7N-2F1T9j789Fs67-2B58xtO1h8BuRMQ5TtQwKT-2BziG5ru-2BiDm1e-2BSp1mX1XSS-2Bp0mysLuGuXiX-2Fhs0d3dR4zu-2FJz6AVTM9YM1DEFFjHlGt14zP6FQEo9nmigiuSR8Jc-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
_______________________________________________<br>swift-evolution mailing list<br><a>swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OWK4tSasaK2n-2FQIIcS9Ug-2FuFXG-2BJ3z6cFMLgm306hDc4-2FjDJQgHAnl-2FMT44TE2aJuilrRvImk1Pbu1cuCtuebtLzdvzh7VtCzeb-2BOJliBlWefi4PrS2Ta7It4QNOtNWAohO-2FS8CQ9Bu3IVUmq-2FW-2FOmmmRl-2FguXp-2FszbpYry1-2FVQSkheR9oYQbkcDKypa8NLRJ3uBRH9Rvm6BM6hwGIQQGvv-2B5B-2BaJPGNfM22K-2FPs1jU-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
</blockquote></div></div></div></div>
</blockquote></div><br></div>