<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="">Thanks everyone for the feedback! I will look at it in detail and address or incorporate what makes sense for the proposed approach. <div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div dir="ltr" class="">Using fully-fledged if/switch-statements as expressions is another proposal and discussion (which already exists).</div></blockquote></div><div class=""><div dir="ltr" class=""><br class=""></div></div><div dir="ltr" class="">can you point me to this? </div><div dir="ltr" class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 13, 2015, at 7:09 AM, Marc Knaup 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=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Using fully-fledged if/switch-statements as expressions is another proposal and discussion (which already exists).<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Regarding the "then" keyword:</div><div class="">What about "do"?</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">let x = if a do b else c</font></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">So the above example would just be another shortcut for</div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">let x = if a { do { b } } else { c }</font><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class="gmail_extra" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""><div class="gmail_quote">On Sun, Dec 13, 2015 at 3:56 PM, Taras Zakharko via swift-evolution<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" 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=""> <span class="Apple-converted-space"> </span>let data = if connection.valid </div><div class=""> <span class="Apple-converted-space"> </span>connection.cached_data </div><div class=""> <span class="Apple-converted-space"> </span>else {</div><div class=""> <span class="Apple-converted-space"> </span>// 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" target="_blank" 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 class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On 13 Dec 2015, at 06:54, Paul Ossenbruggen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""></div></div><div class=""><div class=""><div class="h5"><div style="word-wrap: break-word;" 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" target="_blank" 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" target="_blank" class="">possen@gmail.com</a>> wrote:</div><br class=""><div class=""><div style="word-wrap: break-word;" 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" target="_blank" class="">possen@gmail.com</a>> wrote:</div><br class=""><div class=""><div style="word-wrap: break-word;" 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" target="_blank" class="">swift-evolution@swift.org</a>> wrote:</div><br class=""><div class=""><div style="word-wrap: break-word;" class=""><div class="">1. I would really hate to explain to someone when<span class="Apple-converted-space"> </span><b class="">if</b><span class="Apple-converted-space"> </span>needs a<span class="Apple-converted-space"> </span><b class="">then</b><span class="Apple-converted-space"> </span>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;" 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><span class="Apple-converted-space"> </span>A<span class="Apple-converted-space"> </span><b class="">then</b><span class="Apple-converted-space"> </span>B<span class="Apple-converted-space"> </span><b class="">else</b><span class="Apple-converted-space"> </span>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;" 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 style="white-space: pre-wrap;" class="">        </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="min-height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""></div></div></div><span class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></span></div></blockquote></div><br class=""><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: 0px !important; margin: 0px !important; padding: 0px !important;" class=""></div><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class=""></blockquote></div><br class=""></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=emrIhnP1hIf76Foxxv4NNJQX-2FWhcznESwKBSwD1MEwyAtdWsePOakt0YYV3MzMKT4PoUtygZIoXW0J862PAfcuEyUrJxWX1hbn0yVUWpopg5KGDt8Ubm4Hz22bx6PRQgrSkbP4iz6ooWI0JXo-2FHWaq-2BHVVOKaLuiMOzq4gMMhczaxCWqiskwGmZ6T6zxbKb9F9Xc78-2F6NK4lVthgw0upjraUy92xlFpA0J8A2lVFqn8-3D" alt="" width="1" height="1" border="0" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space"> </span>_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></div></body></html>