<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hmm… that is really interesting. I wonder if we can use optionals to make this work in an elegant way.<div class=""><br class=""></div><div class="">Forget about else for a second. What if there is an operation which says, this is either this value or nil based on whether it meets a condition? Then else (and else if) can be handled by a combination of that operation and the nil-coelecing operator.</div><div class=""><br class=""></div><div class="">I once wrote a small DSL which had this behavior and it was really nice.</div><div class=""><br class=""></div><div class="">What about something like:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let x = value if? condition</div><div class=""><br class=""></div><div class="">x would have value if condition evaluates to true, or it would be nil. If I wanted an else statement:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let x = value if? condition ?? otherValue</div><div class=""><br class=""></div><div class="">Now it works just like the ternary operator, but is IMHO more readable. Note: these can also be chained to give you else if style behavior:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="font-weight: bold; white-space: pre;">        </span>let x = value if? condition ?? otherValue if? otherCondition ?? evenMoreValue</div><div class=""><br class=""></div><div class="">You could optionally (ha!) put things in parentheses as well, which I always end up doing with the ternary:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let x = (value if? condition) ?? (otherValue if? otherCondition) ?? evenMoreValue</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Not a 100% there yet, but I do think it is a good start at something more elegant…</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 5, 2015, at 10:29 AM, possen p <<a href="mailto:possen@gmail.com" class="">possen@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">The current ternary operator, for this example: <br class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">let val = p == 5 ? 10 : 40 <br class=""><br class="">Which I have always thought was hard to read but I do like the functionality it provides. That is, in one expression you can compactly get two different values based upon a condition. Swift seems to have adopted the C style ternary operators probably to not completly change everytihg. Similar to the drop of the ++ and -- operator I am proposing that there is to replace the ternary operator to improve readability but continue to provide that functionality. </font></div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">Recommendation: most readable but changes rules about if-else always having braces and puts the “if” at end. </font><span class="" style="font-family: Menlo;">It is only a little bit longer than the original. </span><font face="Menlo" class="">I think it is clearer to have the conditional at the end so the assignment part is where the variable is assigned. This also does not introduce new keywords or operators. </font></div><div class=""><span class="" style="font-family: Menlo;"><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">let </font><span class="" style="font-family: Menlo;">val</span><font face="Menlo" class=""> = 10 else 40 if p == 5</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class=""><br class="">In looking at the Nil-Coalescing operator there is a similar idea but it is really not the same. In that the left hand side of the ?? operator returns itself when non nil, and the behavior of the ternary operator is different. It is also harder to read. <br class=""><br class="">let </font><span class="" style="font-family: Menlo;">val</span><font face="Menlo" class=""> = 10 ?? 40 if p = 5<br class=""><br class="">I also considered a bunch of other possibilities like using “where" or “when" instead of “if”, the python of putting conditional in the middle or the ruby style of “if" returning a value but did not like those. </font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">// python style</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">let val = 10 if p == 5 else 40 </font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class=""><br class=""></font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">// ruby style</font></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class="">let val = if p == 5 then 10 else 40 </font></div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;"><font face="Menlo" class=""><br class=""></font></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=vmG20pDvfvCvG17-2BDXZuJFPci2-2BE9u9BHImOrEcZwsC0brj-2F-2B1oMAuJJVpJyLz4LrdGcaqxD3YM8Z0wwlNX3FeTItYNojRP7mpIXVzeHgz2ZEyWn-2BESkfG-2BxWXQ6toK57m9s9bLz4gx3F8Ef5RYYezh1e0pzNNz41xOOV5iq-2FXMBr0337pASANjBu7YrfEddI-2FI7EYNJAlb0QrNPb-2BKYwA-3D-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;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>