[swift-evolution] ternary operator ?: suggestion
Paul Ossenbruggen
possen at gmail.com
Sat Jan 16 13:32:11 CST 2016
In reviewing the proposal and looking at the statement form:
public enum Trade {
case Buy(quantity: Double, price: Double)
case Sell(quantity: Double, price: Double)
}
let commissions = trades.map {
case .Buy(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
quantity * price * vipCommissionRate / 100
case .Buy(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
quantity * price * commissionRate / 100
case .Sell(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
quantity * price * vipCommissionRate / 100
case .Sell(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
quantity * price * commissionRate / 100
}
I feel like it needs some syntax to indicate there is a return value for each case:
public enum Trade {
case Buy(quantity: Double, price: Double)
case Sell(quantity: Double, price: Double)
}
let commissions = trades.map {
case .Buy(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
<= quantity * price * vipCommissionRate / 100
case .Buy(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
<= quantity * price * commissionRate / 100
case .Sell(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
<= quantity * price * vipCommissionRate / 100
case .Sell(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
<= quantity * price * commissionRate / 100
}
it would enforce that there is only one <= for each and check that each result is the correct type to prevent errors where multiple return values are generated but the last one is ignored, I suppose that could be a warning though. This would only be used in the “case” form where multiple statements are used. Another option but we loose conciseness is to require return for multi statement expressions, but if you are doing multi statement, then you are probably not as concerned about conciseness:
public enum Trade {
case Buy(quantity: Double, price: Double)
case Sell(quantity: Double, price: Double)
}
let commissions = trades.map {
case .Buy(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
return quantity * price * vipCommissionRate / 100
case .Buy(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
return quantity * price * commissionRate / 100
case .Sell(let quantity, let price) where quantity * price > 10000:
let vipCommissionRate = calculateCommissionRate(...)
return quantity * price * vipCommissionRate / 100
case .Sell(let quantity, let price):
let commissionRate = calculateCommissionRate(...)
return quantity * price * commissionRate / 100
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160116/e7ce66d5/attachment.html>
More information about the swift-evolution
mailing list