[swift-evolution] ternary operator ?: suggestion

Craig Cruden ccruden at novafore.com
Sat Jan 16 16:47:56 CST 2016


I don’t think adding <= which is really just a synonym for `return` would be inconsistent with current closure definitions.

case .Buy(leg quantity, let price) where quantity * price > 10000: 
	let vipCommissionRate = calculateCommissionRate(…)
	quantity * price * vipCommissionRate / 100

is a partial closure.  Based on closure syntax you would be able to write it as a return or not (optional).  If a specific team has a preference I would expect them to have something like a linter.

If we were consistent with closure syntax and not “case” syntax it would actually read “case …. in ….”  but no one seemed to like it (my preference would have been “=>” or “->” instead of in which would be a much clear than “in”…. but that is not an option since it is not Swift syntax).

I personally don’t like the keyword “return” since it just seems so…. procedural…. (i.e. dating back to call …. subroutine….. return….).  I guess each of us has hangups around silly little things…. (let still reminds me of horrid days of having to write visual basic code).





> On 2016-01-17, at 2:32:11, Paul Ossenbruggen <possen at gmail.com> wrote:
> 
> 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/20160117/0454d647/attachment.html>


More information about the swift-evolution mailing list