<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="">I believe the idea with switch is that it would be nice to be able to do something like<div class=""><br class=""></div><div class="">// make sure we have different favorite colors</div><div class="">let myFavoriteColor = </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>switch yourFavoriteColor {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>case .Red:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return .Blue</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>case .Green:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return .Red</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>case .Blue:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>return .Green</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">This is useful and exists in other functional languages. For example, ML has “case … of …”, Coq has “match…with…end”, etc.</div><div class=""><br class=""></div><div class="">This is similar to what you are proposing with if:</div><div class=""><br class=""></div><div class="">let x = if (condition) { return value } else { return otherValue }</div><div class=""><br class=""></div><div class="">because in both cases you are taking what is currently a statement in Swift and enabling it to be used as an expression. (Of course, the syntax would ideally be nicer — maybe we limit each case’s body to be a single expression and get rid of the return keyword, for example.) Worth noting that in both cases, the type-checker needs to ensure that the type of each result is the same.</div><div class=""><br class=""></div><div class="">The semantics of what you’re now proposing — an if? binary operator that evaluates a condition then evaluates to either nil (if false) or some value (if true) — is slightly different. I’m not sure I love it — the general use case would be something like “if __ then ___ else ___” and I don’t think it reads that way. It also doesn’t allow for pattern matching (though to be fair, neither does the ternary operator) — it could be cool to see something like</div><div class=""><br class=""></div><div class="">let greeting = “Hello, \(name),” if let name = optionalName else “To Whom It May Concern:”</div><div class=""><br class=""></div><div class="">-Alex</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 5, 2015, at 6:28 PM, Jonathan Hull 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="">I think I have a solution for the ternary part, but I am not sure I fully understand what is being asked for in the case of switch. Could someone clarify?
<div class="">
<br class="">
</div>
<div class="">
There is another thread which goes into the idea a little more, but the basic idea is that we replace the ternary operator with ‘if?'
</div>
<div class="">
<br class="">
</div>
<div class="">
let x = value if? condition
</div>
<div class="">
<br class="">
</div>
<div class="">
<br class="">
</div>
<div class="">
If condition is true, then x is value… otherwise it is nil. You can combine with the nil-coalescing operator to get else and else if style behavior:
</div>
<div class="">
<br class="">
</div>
<div class="">
let x = valueA if? conditionA ?? valueB if? conditionB ?? valueC
</div>
<div class="">
<br class="">
</div>
<div class="">
<br class="">
</div>
<div class="">
Thanks,
</div>
<div class="">
Jon
</div>
<div class="">
<br class="">
</div>
<div class="">
<br class="">
<div class="">
<blockquote type="cite" class="">
<div class="">
On Dec 5, 2015, at 9:09 AM, Chris Lattner <
<a href="mailto:clattner@apple.com" class="">clattner@apple.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="">
<br class="">
<div class="">
<blockquote type="cite" class="">
<div class="">
On Dec 4, 2015, at 11:43 AM, J. Cheyo Jimenez <
<a href="mailto:cheyo@masters3d.com" class="">cheyo@masters3d.com</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="">
<div class="">
I'd like some feedback from the core team to see if eliminating the ternary operator and replacing it with if-else is even proposal worthy.
</div>
<div class="">
<br class="">
</div>
<div class="">
Some interesting code in the standard library that uses the ternary operator quite a bit.
</div>
<div class="">
<br class="">
</div>
<a href="https://gist.github.com/masters3d/7b0c6f3653368afe4357" class="">https://gist.github.com/masters3d/7b0c6f3653368afe4357</a>
<br class="">
</div>
</div>
</blockquote>
<br class="">
</div>
<div class="">
FWIW, I have no love for the ternary operator (it is totally "bizarre C magic”), but it does solve a problem that Swift currently otherwise has no solution for.
</div>
<div class="">
<br class="">
</div>
<div class="">
If you’re interested in pursuing this, then the right path forward is to build a holistic proposal to turn statements (like if and switch) into expressions. This is subtle and requires thought, but is widely desired and if someone were interested in driving the design and implementation, then I’d be interested to see it happen.
</div>
<div class="">
<br class="">
</div>
<div class="">
-Chris
</div>
<br 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a>
<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
<div style="margin-top:10px;font-size:12px;font-family:Helvetica,Arial;color:#999;" class="">
Untracked with
<a style="color:#999;" href="https://trackbuster.com/?sig" class="">Trackbuster</a>
</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=""></div></body></html>