<div dir="ltr">Using fully-fledged if/switch-statements as expressions is another proposal and discussion (which already exists).<div><br></div><div>The replacement of the ternary operator should be just syntactic sugar for the other proposal to have a shorter version of "let x = if a {} else {}".</div><div><br></div><div>Regarding the "then" keyword:</div><div>What about "do"?</div><div><br></div><div><font face="monospace, monospace">let x = if a do b else c</font></div><div><br></div><div>Yes, it reads a bit weird, but as if & switch statements are moving towards being expressions already then "do" blocks will likely too.</div><div><br></div><div>So the above example would just be another shortcut for</div><div><br></div><div><font face="monospace, monospace">let x = if a { do { b } } else { c }</font><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Dec 13, 2015 at 3:56 PM, Taras Zakharko via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</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">Hi Paul, <div><br></div><div> 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><br></div><div> let data = if connection.valid </div><div> connection.cached_data </div><div> else {</div><div> // generate the data again</div><div> } <br><div><br></div><div> 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" target="_blank">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><br></div><div> func if_<T>(condition: Bool, then_: (()->T)?, else_: (()->T)?) -> T? {</div> var result: T?<br> <br> if condition {<br> result = then_?()<br> } else {<br> result = else_?()<br> }<br> <br> return result<br>}<div><br></div><div>the compiler would then translate all if statements into</div><div><br></div><div> if_(condition, then_: then block, else_: else block)</div><div><br></div><div>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><br></div><div> let x = if_(condition, then_: then block, else_: else block)!</div><div><br></div><div>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><br></div><div>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><br></div><div>The switch expression can be approached in a similar way, but would require more compiler magic. </div><div><br></div><div>Best, </div><div><br></div><div> Taras</div><div><br></div></div><div><blockquote type="cite"><div><div class="h5"><div>On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br></div></div><div><div><div class="h5"><div style="word-wrap:break-word"><div>Hello All, </div><div><br></div><div>Been sick in bed all day, but decided to try to be productive…</div><div><br></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><br></div><div><a href="https://github.com/possen/swift-evolution/blob/master/0020.md" target="_blank">https://github.com/possen/swift-evolution/blob/master/0020.md</a></div><div><br></div><div>Thanks,</div><div>- Paul</div><div><br><div><br></div><div><br><div><blockquote type="cite"><div>On Dec 12, 2015, at 3:51 PM, Paul Ossenbruggen <<a href="mailto:possen@gmail.com" target="_blank">possen@gmail.com</a>> wrote:</div><br><div><div style="word-wrap:break-word">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><br></div><div>let x = If cond then X else Y</div><div><br></div><div>is the full form, where “else" can not be omitted. </div><div><br></div><div><blockquote type="cite"><div>On Dec 12, 2015, at 12:59 PM, Paul Ossenbruggen <<a href="mailto:possen@gmail.com" target="_blank">possen@gmail.com</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div><br></div><br><div><blockquote type="cite"><div>On Dec 12, 2015, at 12:37 PM, Andrey Tarantsov via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word"><div>1. I would really hate to explain to someone when <b>if</b> needs a <b>then</b> and when it doesn't. That's the sort of inconsistency that shouldn't be added lightly.</div></div></div></blockquote><div><br></div><div>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><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div>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>if</b> A <b>then</b> B <b>else</b> C).</div></div></div></blockquote><div><br></div>Yes that would be great.</div><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div><br></div><div>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><br></div><div>The biggest problem statement is you don’t need to exhaustively specify every outcome:</div><div><br></div><div>if cond {</div><div><span style="white-space:pre-wrap">        </span>print(“hello”)</div><div>}</div><div><br></div><div>whereas in an expression you have to specify what happens in the else.</div><div><br></div><div>let say = if cond then “hello” else “goodbye"</div></div><div><br></div><div>unless you go seriously off the deep end:</div><div><br></div><div>let say = if cond then “hello” </div><div><br></div><div> “say" then becomes an optional, *shudder*</div><div><br></div><div><br></div></div></div></blockquote></div><br></div></div></blockquote></div><br></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="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></div></div><span class="">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">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></span></div></blockquote></div><br>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=6ZGE61OxINd5lLe2xYh9Ku-2BXbixWNr2nvfzp2IB1sZhI5nghOohh4ybx9EH-2Byim-2Bm2sQO3l3Icl2WHooXqjp66RO3F1eYvd-2B6g4VBYT4Ey6J3AzB-2B2FCZnh-2FZV21jpjHMehCiz77Vtm35bstW4TP6vd76HEYpifHsvJuYzQGj4FrtnXvxxsacr9gBL1PqQ-2FjVE6OC-2B-2F7YoqiFooSjemvb0N-2F-2Bx2OKfusRlYGjVHvqCc-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>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
<br></blockquote></div><br></div>