<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>-1</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I love the idea of challenging the syntax but without any real benefit I can see it harder for new devs and it will cause breaking changes that doesn't outweigh the cause. The syntax z ? x : y is not hard to comprehend and expresses powerful statements in just a simple line. I do agree it is abused. It is used in places that a fuller if statement would help make sense of the code. It is on the developer to help make the operator easier to understand. The operator is the same across many languages which helps create a standard. <br><br>Alvarado, Joshua</div><div><br>On Oct 26, 2016, at 9:12 AM, Mark Sands via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">Strong <b>+1</b> from me.<div><br></div><div>This simply feels like the right approach for Swift, as we see the language head in a direction that has abandoned traditional C-style idioms. As swift has already dropped support for the ++/-- operators and C-style for loops it makes logical sense that dropping the ternary operator (or replacing with a more Swift-like idiom) should follow.</div><div><br></div><div>As a side note, after upgrading my swift code to Swift 3, I feel as though I've become un-phased at future source breaking changes until full stability is met and set in stone. If they're worth it, bring them on, I say.</div><div><br></div><div>Mark</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 26, 2016 at 8:52 AM, Mike Kasianowicz 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">I like the idea in theory, but I also like the existing ternary operator. Could some of this be accomplished without drastic changes?<div><br></div><div>Some alternative ideas-</div><div><br></div><div>1) Codify ternary operator declaration in the language, but make some common-sense restrictions to reduce expression parsing complexity (no line breaks, enforced parens, 'simple' arguments, stuff like that). If there were an extension like you propose in addition, my preference would be a verb like "select".</div><div><br></div><div>2) Make it a binary operator with autoclosure tuple on the RHS (is it possible to put autoclosure within a tuple?):</div><div><br></div><div><div style="color:rgb(0,0,0);font-size:14.4px">public static func ?<T>(_ value: Bool, _ branches: (_ t: @autoclosure () -> T, _ f: @autoclosure () -> T)) -> T {</div><div style="color:rgb(0,0,0);font-size:14.4px"> if value {</div><div style="color:rgb(0,0,0);font-size:14.4px"> return branches.t()</div><div style="color:rgb(0,0,0);font-size:14.4px"> } else {</div><div style="color:rgb(0,0,0);font-size:14.4px"> return branches.f()</div><div style="color:rgb(0,0,0);font-size:14.4px"> }</div><div style="color:rgb(0,0,0);font-size:14.4px"> }</div></div><div><div><div><br></div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="gmail-h5">On Tue, Oct 25, 2016 at 11:51 PM, Charlotte Angela Tortorella via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br></div></div><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><div class="gmail-h5"><div style="word-wrap:break-word">Preamble: I've read over the threads that already exist about the ternary operator and to be honest they're a complete mess without a single fully formed proposal.<div><br></div><div>Pitch: I'd like to simplify the syntax, compiler complexity and learning curve for newcomers when it comes to dealing with the ternary function. The best way to do that, in my opinion, is to remove it entirely and add a new function with better semantics that takes care of ternary operations entirely within the Swift language.</div><div><br></div><div>gist: <a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c" target="_blank">https://gist.github.com/<wbr>Qata/25a11c21200f1cf8f43ed78e9<wbr>ffd727c</a></div><div><br></div><div><div>Replace the `?:` operator with an in-language function</div><div><br></div><div>Proposal: TBD</div><div>Author: [Charlotte Tortorella](<a href="https://github.com/qata" target="_blank">https://github.com<wbr>/qata</a>)</div><div>Editor: [Soroush Khanlou](<a href="https://github.com/khanlou" target="_blank">https://github.com/kh<wbr>anlou</a>)</div><div>Review Manager: TBD</div><div>Status: TBD</div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#introduction" target="_blank">Introduction</a></div><div><br></div><div>The ternary operator in Swift was added early in development, as a holdover</div><div>from C. This document is an attempt to provide a clear look at the ternary</div><div>operator without the baggage of the languages that came before, and comes</div><div>to the conclusion that we should deprecate and remove the ternary operator</div><div>in favor of an extension to `Bool`.</div><div><br></div><div>As a quick refresher, here's what the ternary operator looks like:</div><div><br></div><div>let a = 10</div><div>let b = 20</div><div>// If a is less than b, sets e to "foo", else sets e to "bar"</div><div>let e = a < b ? "foo" : "bar"</div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#advantages-of-the-ternary-operator" target="_blank">Advantages of The Ternary Operator</a></div><div><br></div><div>The primary advantage of this operator is its terseness and expressive</div><div>capability. It's shorthand for (e.g.):</div><div><br></div><div>let a = 10</div><div>let b = 20</div><div>let e: String</div><div>if a < b {</div><div> e = "foo"</div><div>} else {</div><div> e = "bar"</div><div>}</div><div><br></div><div>The second advantage of Swift supporting the ternary operator is continuity</div><div>with C, and other common languages in the extended C family (C++, Objective-C,</div><div>Java, C#, Javascript, etc). People coming to Swift from these other languages</div><div>may reasonably expect this operator to exist. That said, there are also</div><div>popular languages which have kept the majority of C operators but dropped the</div><div>ternary operator (e.g. [Go](<a href="https://golang.org/doc/faq#Does_Go_have_a_ternary_form" target="_blank">https://golang.org/doc/fa<wbr>q#Does_Go_have_a_ternary_form</a>) and [Rust](<a href="https://github.com/rust-lang/rfcs/issues/1362" target="_blank">https://github.com/rust<wbr>-lang/rfcs/issues/1362</a>)).</div><div><br></div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#disadvantages-of-the-ternary-operator" target="_blank">Disadvantages of The Ternary Operator</a></div><div><br></div><div>1. The existence of the ternary operator as a holdover from C is to increase</div><div>the familiarity of the Swift language for C family developers, at the expense</div><div>of newcomers. Established developers do much better with learning concepts</div><div>than newcomers to programming and probably don't need their hands held</div><div>with this carry over of an operator.</div><div><br></div><div>2. The ternary operator adds complexity to the compiler, because it requires</div><div>special handling. It is the only operator that requires two components to</div><div>work (both the `?` and the `:`), it uses a character that is excluded from</div><div>being used in other operators (`:`), and it isn't defined in the standard</div><div>library.</div><div><br></div><div>3. The ternary operator's usage of `?` can be confusing</div><div>to new users. Every other instance of `?` is associated with</div><div>`Optional` values.</div><div><br></div><div>4. The ternary operator uses `:`, which is already a heavily overloaded</div><div>symbol in Swift. `:` is used in hash tables, type annotations for variables,</div><div>class inheritance, and protocol conformance.</div><div><br></div><div>5. The ternary operator's short length lends it to being abused in the</div><div>nested ternary operator anti-pattern. This is similar to the `++` and</div><div>`--` operators, which were removed in Swift 3. While they worked fine and were</div><div>readable enough when used alone, using them multiple times in a single</div><div>expression like `function(a++, ++a)` made them highly unreadable and</div><div>confusing.</div><div><br></div><div>6. This operator is only applicable to a single type, `Bool`.</div><div><br></div><div>7. If the ternary operator weren't in common usage, it would not be proposed</div><div>for Swift. Higher clarity can be achieved with common language features by</div><div>creating an extension to `Bool`.</div><div><br></div><div>8. The ternary operator was created for and is much more suited to a language</div><div>like C, where there were no generics and as such no alternative to an</div><div>unintuitive operator.</div><div><br></div><div>9. Several other modern languages, like Rust and Go discussed earlier, have</div><div>eschewed the usage of the ternary operator entirely. Other languages that have</div><div>special constructs similar to `?:`, such as `if then else` in Haskell have</div><div>[discussed removing it](<a href="https://wiki.haskell.org/If-then-else#Is_If-Then-Else_so_important.3F" target="_blank">https://wiki.haskell.org/I<wbr>f-then-else#Is_If-Then-Else_so<wbr>_important.3F</a>). `if then else` is identical to the `?:` operator,</div><div>excepting that it's prefixed by `if`, while `?:` has no prefix.</div><div><br></div><div> Example: `if True then 10 else 20`</div><div><br></div><div>10. On a more personal and anecdotal note, the ternary operator gave me more</div><div>trouble than any other operator when I was first learning how to program.</div><div>I’ve also spoken to several other people who expressed similar sentiments</div><div>about this operator’s inscrutability.</div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#proposed-approach" target="_blank">Proposed Approach</a></div><div><br></div><div>We should drop the ternary operator in favor of a new extension to `Bool`.</div><div>There are a few possibilities for the naming of this function. We've provided</div><div>four for consideration in this proposal, but are open to other options as well.</div><div>This proposal is much more about the concept than the naming of the replacement</div><div>function.</div><div><br></div><div>extension Bool {</div><div> /// If `self == true`, returns `t`, otherwise, returns `f`.</div><div> func transformed<T>(true t: @autoclosure () -> T, false f: @autoclosure () -> T) -> T {</div><div> if self {</div><div> return t()</div><div> } else {</div><div> return f() </div><div> }</div><div> }</div><div><br></div><div> func when<T>(true t: @autoclosure () -> T, false f: @autoclosure () -> T) -> T {</div><div> ...</div><div> }</div><div><br></div><div> func if<T>(true t: @autoclosure () -> T, false f: @autoclosure () -> T) -> T {</div><div> ...</div><div> }</div><div><br></div><div> func if<T>(then t: @autoclosure () -> T, else f: @autoclosure () -> T) -> T {</div><div> ...</div><div> }</div><div>}</div><div><br></div><div>Only one of these should be chosen. We're not proposing adding multiple</div><div>functions that achieve the same thing.</div><div><br></div><div>Example usage:</div><div><br></div><div>let a = 10</div><div>let b = 20</div><div>_ = (a < b).transformed(true: "foo", false: "bar")</div><div>_ = (a < b).when(true: "foo", false: "bar")</div><div>_ = (a < b).if(true: "foo", false: "bar")</div><div>_ = (a < b).if(then: "foo", else: "bar")</div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#impact-on-existing-code" target="_blank">Impact on existing code</a></div><div><br></div><div>This proposal is breaking and would require migration.</div><div><br></div><div><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#alternatives-considered" target="_blank">Alternatives considered</a></div><div><br></div><div>Simplest alternative: we could leave the ternary operator as is and not</div><div>introduce any new concepts.</div><div><br></div><div>It'd also be possible to add an `if then else` Haskell-esque expression.</div><div>This would have the disadvantages of still needing special handling by the</div><div>compiler. Since this proposal's intention is partially to remove compiler</div><div>complexity, this would be counterproductive and would probably confuse new</div><div>users in a similar way to how `?:` does.</div></div><div><br></div></div><br></div></div><span class="gmail-">______________________________<wbr>_________________<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" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br></span></blockquote></div><br></div>
<br>______________________________<wbr>_________________<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/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></body></html>