[swift-evolution] ternary operator ?: suggestion
Paul Ossenbruggen
possen at gmail.com
Thu Jan 7 10:16:43 CST 2016
Like the switch statement, I think it would help to at least optionally be able to have parenthesis around the cases. It gives it a more structure that I think the “case” and “default” need and it would encourage indenting which I think helps a lot. But I think it might not be a bad thing to require the parenthesis. for the switch like flavor. I think parenthesis are better because it is an expression and it should feel more like a function, but that is open for debate.
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
)
But if we go with brackets then I would recommend something like this:
let commission = match (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
}
Which is much more statement like and may make the expression vs statement distinction harder to see and explain. And we lose terseness for the simple single line case. Anyway, I think the cases need to be indented and have some indication when they are complete to add balance.
> On Jan 7, 2016, at 2:14 AM, Craig Cruden <ccruden at novafore.com> wrote:
>
> If the `where` clause is omitted it would make the following sample code a little more complex than it would need to be:
>
> public enum Trade {
> case Buy(quantity: Int, price: Double)
> case Sell(quantity: Int, price: Double)
> }
>
> let vipCommissionRate = 1.25
> let commissionRate = 2.50
>
> let trade = Trade.Buy(quantity: 400, price: 12.50)
>
> let commission: Double
>
> 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
>
>
>
>> On 2016-01-07, at 15:19:48, Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>>
>> On the where/if conditional clause being omitted…. if that is done it will lead to more convoluted code since people will end up nesting in ternary below the “switch/match” to accomplish that where a simple filter would have left it at one level of code.
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160107/15c925b0/attachment.html>
More information about the swift-evolution
mailing list