<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>I still find this backward. </div><div id="AppleMailSignature">Maybe from years of other languages, except Ruby where this would be a one liner. </div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Usually the conditional is before the assignment and that's how it is executed AFAIK<br><br>Sent from my iPhone</div><div><br>On Jan 5, 2016, at 4:16 PM, Charles Constant via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Thanks Austin,<div><br></div><div><div><b>let myThing = {</b></div><div><b> if someCondition {</b></div><div><b> return 1</b></div><div><b> } else {</b></div><div><b> return 2</b></div><div><b> }</b></div><div><b>}()</b></div></div><div><br></div><div>While that is ugly, it does have the benefit of being crystal clear about its intent (i.e.: setting "myThing") since everything after the first line is going to be indented.</div><div><br></div><div>What I dislike about using a "switch" to achieve the same thing is rereading my code. It can take a couple moments to clue in that its just assigning, and to realize there's nothing *else* going on in the "switch" aside from the assignment. </div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 4, 2016 at 11:04 PM, Austin Zheng <span dir="ltr"><<a href="mailto:austinzheng@gmail.com" target="_blank">austinzheng@gmail.com</a>></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"><span class="">There was a popular pattern which went as follows:<div><br></div><div>let myThing = {</div><div> if someCondition {</div><div> return 1</div><div> } else {</div><div> return 2</div><div> }</div><div>}()</div><div><br></div><div>It's a little ugly, but the compiler is apparently smart enough to inline/optimize away the anonymous closure. It became less popular once Swift got support for declaring lets without an initial value.</div><div><br></div><div>Austin</div><div><br></div></span><div><blockquote type="cite"><span class=""><div>On Jan 4, 2016, at 10:55 PM, Charles Constant via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></span><span class=""><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><span style="font-size:13px">My best guesses here, since I didn't write and don't entirely agree...</span></div><div><span style="font-size:13px"><br></span></div><div><b><span style="font-size:13px">> </span><span style="font-size:13px">Please detail what the trade offs are</span></b></div><span style="font-size:13px"><div><span style="font-size:13px"><br></span></div><div>Other than more complexity, I think this refers to making the "switch" statement do two slightly different things. Of course, if we call it something else, like "match" or "which" then it stops being an issue.</div><div><span style="font-size:13px"><br></span></div><b>> What are the other ways that Swift currently supports this?</b></span><br><div><span style="font-size:13px"><br></span></div><div>As far as I can see, the closest we get are...</div><div><br></div><div> a) The existing "switch" statement, or if else statement (preceded with a statement to declare the variable. yuck). I think this is verbose enough to qualify as "confusing"</div><div><br></div><div> b) Creating an anonymous dict, and immediately accessing it (which means the keys need to be Hashable). This isn't very flexible</div><div><br></div><div> c) Creating a special init or function, to map one enum, to another. This is also verbose, and not flexible, and moves the code away from where it used (even if you never reuse the mapping).</div><div><br></div><div> d) Chaining a bunch of conditions in a ternary... Horrible.</div><div><br></div><div>Anyone know of any better alternatives? It feels like I'm missing something.</div><div><br></div></div></div></span></blockquote></div><br></div></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=1UuF0lDdi-2BUHzKWDs6hnDkbjNauJshI0mDryye6koZH1KeQ7H9dk-2Fvg89ebun1DUOSDfyMvafVrMAsEUGjPWKSZVsFZu2R9zSc-2BP-2FO-2BG82tblL8fTk-2Brb3zw4MUmjRdl4CsleLsid-2B7PevSSV-2FnaJ-2BCfr7q84t1UQ3LQreCz1vvmlKWM2hnAyu9Acwxtltewqufs-2BBv4HTzhneg11tutIkYqfpxxOkmIGJiguv1eJQI-3D" alt="" width="1" height="1" border="0" style="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 href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>