<div dir="ltr"><span style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">I believe the idea with switch is that it would be nice to be able to do something like</span><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">// make sure we have different favorite colors</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">let myFavoriteColor = </div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">        </span>switch yourFavoriteColor {</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>case .Red:</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>return .Blue</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>case .Green:</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>return .Red</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>case .Blue:</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">                </span>return .Green</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><span style="white-space:pre-wrap">        </span>}</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">This is useful and exists in other functional languages. For example, ML has “case … of …”, Coq has “match…with…end”, etc.</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">This is similar to what you are proposing with if:</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">let x = if (condition) { return value } else { return otherValue }</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">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 style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">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 style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">let greeting = “Hello, \(name),” if let name = optionalName else “To Whom It May Concern:”</div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px"><br></div><div style="color:rgb(0,0,0);font-family:Helvetica;font-size:12px">-Alex</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 5, 2015 at 6:28 PM, Jonathan Hull 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">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>
<br>
</div>
<div>
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><span class="">
<div>
<br>
</div>
<div>
let x = value if? condition
</div>
<div>
<br>
</div>
<div>
<br>
</div>
</span><div>
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>
<br>
</div>
<div>
let x = valueA if? conditionA ?? valueB if? conditionB ?? valueC
</div>
<div>
<br>
</div>
<div>
<br>
</div>
<div>
Thanks,
</div>
<div>
Jon
</div>
<div>
<br>
</div>
<div>
<br>
<div>
<blockquote type="cite">
<div>
On Dec 5, 2015, at 9:09 AM, Chris Lattner <
<a href="mailto:clattner@apple.com" target="_blank">clattner@apple.com</a>> wrote:
</div>
<br>
<div>
<div style="word-wrap:break-word">
<br>
<div>
<blockquote type="cite">
<div>
On Dec 4, 2015, at 11:43 AM, J. Cheyo Jimenez <
<a href="mailto:cheyo@masters3d.com" target="_blank">cheyo@masters3d.com</a>> wrote:
</div>
<br>
<div>
<div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
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>
<br>
</div>
<div>
Some interesting code in the standard library that uses the ternary operator quite a bit.
</div>
<div>
<br>
</div>
<a href="https://gist.github.com/masters3d/7b0c6f3653368afe4357" target="_blank">https://gist.github.com/masters3d/7b0c6f3653368afe4357</a>
<br>
</div>
</div>
</blockquote>
<br>
</div>
<div>
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>
<br>
</div>
<div>
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>
<br>
</div>
<div>
-Chris
</div>
<br>
</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>
</div>
<div style="margin-top:10px;font-size:12px;font-family:Helvetica,Arial;color:#999">
Untracked with
<a style="color:#999" href="https://trackbuster.com/?sig" target="_blank">Trackbuster</a>
</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>