<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="">Hi Paul, <div class=""><br class=""></div><div class=""> what bothers me in your proposal is that you seem to allow only simple expressions. But it is often the case that one needs to have multiple statements to set up the value. For example:</div><div class=""><br class=""></div><div class=""> let data = if connection.valid </div><div class=""> connection.cached_data </div><div class=""> else {</div><div class=""> // generate the data again</div><div class=""> } <br class=""><div class=""><br class=""></div><div class=""> I have been thinking a bit about how to implement this in connection with the idea of code blocks as closures proposal I have posted earlier (<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/002056.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20151207/002056.html</a>). Switch is a bit more complicated, but I think that the existing if statement can be easily turned into expression:</div><div class=""><br class=""></div><div class=""> func if_<T>(condition: Bool, then_: (()->T)?, else_: (()->T)?) -> T? {</div> var result: T?<br class=""> <br class=""> if condition {<br class=""> result = then_?()<br class=""> } else {<br class=""> result = else_?()<br class=""> }<br class=""> <br class=""> return result<br class="">}<div class=""><br class=""></div><div class="">the compiler would then translate all if statements into</div><div class=""><br class=""></div><div class=""> if_(condition, then_: then block, else_: else block)</div><div class=""><br class=""></div><div class="">if the else block is not present, it will be set to nil. In that case the if expression also evaluates to nil. Now, if the if is used as a statement, the above transformation is sufficient and the return value will be optimised away by the compiler. If the expression form is used (i.e. there is an assignment operation), the compiler will forcefully unwrap the result:</div><div class=""><br class=""></div><div class=""> let x = if_(condition, then_: then block, else_: else block)!</div><div class=""><br class=""></div><div class="">This way, if else block is absent, the program will crash. A bit of tweaking will also generate a useful error message. I am also sure that it is possible to generate a warning (or even an error) at the compile time without too much effort. </div><div class=""><br class=""></div><div class="">I think the nice thing about this proposal is that it uses already existing mechanisms in the language and only requires some minimal transformations by the compiler. </div><div class=""><br class=""></div><div class="">The switch expression can be approached in a similar way, but would require more compiler magic. </div><div class=""><br class=""></div><div class="">Best, </div><div class=""><br class=""></div><div class=""> Taras</div><div class=""><br class=""></div></div><div><blockquote type="cite" class=""><div class="">On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</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="">Hello All, </div><div class=""><br class=""></div><div class="">Been sick in bed all day, but decided to try to be productive…</div><div class=""><br class=""></div>I did a rough draft of a proposal for implementing if expressions and switch expressions based upon the discussions we had here. I have tried to keep the scope of the changes as small as possible, only added one keyword and kept things as similar to the existing language constructs as possible. If anyone wants to help me with this, or has feedback, please let me know,<div class=""><br class=""></div><div class=""><a href="https://github.com/possen/swift-evolution/blob/master/0020.md" class="">https://github.com/possen/swift-evolution/blob/master/0020.md</a></div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">- Paul</div><div class=""><br class=""><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2015, at 3:51 PM, Paul Ossenbruggen <<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="">Implied in using the “then", if…then…else would aways require “else" when using “then” similar to how “guard" requires “else”. This will help to make the difference between statements and expressions clear.<div class=""><br class=""></div><div class="">let x = If cond then X else Y</div><div class=""><br class=""></div><div class="">is the full form, where “else" can not be omitted. </div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2015, at 12:59 PM, Paul Ossenbruggen <<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=""><br class=""></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 12, 2015, at 12:37 PM, Andrey Tarantsov via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</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="">1. I would really hate to explain to someone when <b class="">if</b> needs a <b class="">then</b> and when it doesn't. That's the sort of inconsistency that shouldn't be added lightly.</div></div></div></blockquote><div class=""><br class=""></div><div class="">agreed definitely want to be careful with that. I think with braces meaning statements that differentiation can be made clear. I would certainly start with statements when describing, just as you usually don’t talk about the ternary operator until later. </div></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">3. If we can somehow solve all of this, I think I'll be +1 for replacing (A ? B : C) with some sort of (<b class="">if</b> A <b class="">then</b> B <b class="">else</b> C).</div></div></div></blockquote><div class=""><br class=""></div>Yes that would be great.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">4. Generally, I wonder how hard would it be for all statements to be usable as expressions? Why didn't Swift go that way from the start?</div></div></div></blockquote><div class=""><br class=""></div><div class="">The biggest problem statement is you don’t need to exhaustively specify every outcome:</div><div class=""><br class=""></div><div class="">if cond {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>print(“hello”)</div><div class="">}</div><div class=""><br class=""></div><div class="">whereas in an expression you have to specify what happens in the else.</div><div class=""><br class=""></div><div class="">let say = if cond then “hello” else “goodbye"</div></div><div class=""><br class=""></div><div class="">unless you go seriously off the deep end:</div><div class=""><br class=""></div><div class="">let say = if cond then “hello” </div><div class=""><br class=""></div><div class=""> “say" then becomes an optional, *shudder*</div><div class=""><br class=""></div><div class=""><br class=""></div></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=x4DBtcYZPuS8wt8fMTVYDXzwU9szxEmdHE1DwQfVzUcr5Mz9H1m19EJNYODSbR8NnR7w32BR55gN5FPLaQg-2B7SElKofLMNrVa9aU-2Fg-2BVZiqgNY-2FTkpPKt9Tnpy-2Flq6kG5idc-2FW5qzP6qN8FY41Sl-2FDGEPCsuBH47WKHg2S5-2F6rmAtK-2FopYYVlxhu-2FVs0x3ITYruk9xsynFe7vgLB7UrRNLa-2Ffx2pIrvki6oo-2BW34LUM-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=""></body></html>