<div>+1 on this. <span></span></div><div><br></div><div><div><font size="2"><span style="background-color:rgba(255,255,255,0)">&quot;This is also why I think I expressions and statements should be different. Trying to make statements into expressions will lead to a lot of complications and will lead to harder to understand programming model. Not saying it is impossible but definitely much more involved. &quot;</span></font></div></div><div><br></div><div><br></div><br><br>On Sunday, December 13, 2015, Paul Ossenbruggen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; 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"><div>Hi Taras,</div><div><br></div>I understand your point, if you need to do what you are suggesting, which is multiple statements which, eventually resolve to one result, You would still be able to do that with the existent switch statement. The expression syntax should be thought of as more functional in that you don’t have side effects and other processing in it. It simply flows through and produces one answer as any other expression would. <div><br></div><div>Another way to think of it, is if you think you should be able to do it with a ternary operator, then you should be able to do it with the proposed solution. <br><div><br><div><blockquote type="cite"><div>On Dec 13, 2015, at 6:53 AM, Taras Zakharko &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;taras.zakharko@googlemail.com&#39;);" target="_blank">taras.zakharko@googlemail.com</a>&gt; wrote:</div><br><div><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_&lt;T&gt;(condition: Bool, then_: (()-&gt;T)?, else_: (()-&gt;T)?) -&gt; 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></div></div></blockquote><div><br></div><div>Your if_ func is interesting, I am going play with this a bit.</div><div><br></div><blockquote type="cite"><div><div style="word-wrap:break-word"><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></div></div></blockquote><div><br></div>Unless I am misunderstanding your suggestion, I experimented with leaving “else” off resulting in an optional if not all cases are handled. This I think makes it confusing because now you have to deal with an optional, optionals usually mean more conditionals later. I want to know at the end of the expression that my variable was properly set in all cases. It seems like trouble to deal with the possibility that you don’t know the result of the expression at the end of the expression, which is essentially what you have with the nil result and everything after that has to deal with that optional. Doesn’t that mean that all if expressions would return some sort of optional?</div><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><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></div></div></blockquote><div><br></div>It would be preferred to not have runtime errors or compiler errors to deal with. My proposal, by guaranteeing a result, would not have that issue. This is also why I think I expressions and statements should be different. Trying to make statements into expressions will lead to a lot of complications and will lead to harder to understand programming model. Not saying it is impossible but definitely much more involved. </div><div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><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<br><div><br></div><div> </div><div><div><blockquote type="cite"><div>On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><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 &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;possen@gmail.com&#39;);" target="_blank">possen@gmail.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Implied in using the  “then&quot;, if…then…else would aways require “else&quot; when using “then” similar to how “guard&quot; 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&quot; can not be omitted. </div><div><br></div><div><blockquote type="cite"><div>On Dec 12, 2015, at 12:59 PM, Paul Ossenbruggen &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;possen@gmail.com&#39;);" target="_blank">possen@gmail.com</a>&gt; 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 &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" target="_blank">swift-evolution@swift.org</a>&gt; 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&#39;t. That&#39;s the sort of inconsistency that shouldn&#39;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&#39;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&#39;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&quot;</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&quot; 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>
_______________________________________________<br>swift-evolution mailing list<br><a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" 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></div></blockquote></div><br></div></div></div></div></div></blockquote></div><br></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=OWK4tSasaK2n-2FQIIcS9Ug-2FuFXG-2BJ3z6cFMLgm306hDfb5R03K1C75u8smrj26zU13Sjv0LLIASWjCZ-2FLepKZItahGeV8sIkOgZs9Qd5m5riAh6UyeboWKMFhIiACpRhi0nkgKwHhI3uza8FHeW15nKy80z-2F2R7PXKbLeGlfkr6fAkA1TePV-2FvwWxPziUvoNfc9jfEqPGQZiRaGD1Cj2HEkuDq8Ena0BHAEg-2B-2BsvrAKA-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>