[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Thu Jan 7 04:39:14 CST 2016


Craig, golly just looking at your example depresses me. I'm not saying it's
contrived, it seems like real enough code. With the combination of our
proposals, and the requirement to explicitly cast to Double... ugh, my eyes
just glaze over.

Here's "where"...

let commission = trade ?
case .Buy(let quantity, let price) where Double(quantity) * price > 10000:
  Double(quantity) * price * vipCommissionRate / 100
case .Buy(let quantity, let price):
Double(quantity) * price * commissionRate / 100
case .Sell(let quantity, let price) where Double(quantity) * price > 10000:
Double(quantity) * price * vipCommissionRate / 100
case .Sell(let quantity, let price):
    Double(quantity) * price * commissionRate / 100

I also tried my favourite proposal:

let commission = trade ?
.Buy(let quantity, let price) !
Double(quantity) * price > 10000 ?
Double(quantity) * price * vipCommissionRate / 100 :
Double(quantity) * price * commissionRate / 100 :
.Sell(let quantity, let price) !
Double(quantity) * price > 10000  ?
Double(quantity) * price * vipCommissionRate / 100 :
Double(quantity) * price * commissionRate / 100

Both of these are unreadable. I think my preferred example is slightly
unreadable *when it's indented* the way I have here, but it would be a
complete and total disaster as a single line... which people tend to do
with ternary expressions :(


​
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/75907ed8/attachment.html>


More information about the swift-evolution mailing list