[swift-evolution] ternary operator ?: suggestion
Alex Lew
alexl.mail at gmail.com
Sat Dec 5 18:09:46 CST 2015
I believe the idea with switch is that it would be nice to be able to do something like
// make sure we have different favorite colors
let myFavoriteColor =
switch yourFavoriteColor {
case .Red:
return .Blue
case .Green:
return .Red
case .Blue:
return .Green
}
This is useful and exists in other functional languages. For example, ML has “case … of …”, Coq has “match…with…end”, etc.
This is similar to what you are proposing with if:
let x = if (condition) { return value } else { return otherValue }
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.
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
let greeting = “Hello, \(name),” if let name = optionalName else “To Whom It May Concern:”
-Alex
> On Dec 5, 2015, at 6:28 PM, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
>
> 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?
>
> 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?'
>
> let x = value if? condition
>
>
> 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:
>
> let x = valueA if? conditionA ?? valueB if? conditionB ?? valueC
>
>
> Thanks,
> Jon
>
>
>> On Dec 5, 2015, at 9:09 AM, Chris Lattner < clattner at apple.com <mailto:clattner at apple.com>> wrote:
>>
>>
>>> On Dec 4, 2015, at 11:43 AM, J. Cheyo Jimenez < cheyo at masters3d.com <mailto:cheyo at masters3d.com>> wrote:
>>>
>>> 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.
>>>
>>> Some interesting code in the standard library that uses the ternary operator quite a bit.
>>>
>>> https://gist.github.com/masters3d/7b0c6f3653368afe4357 <https://gist.github.com/masters3d/7b0c6f3653368afe4357>
>>
>> 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.
>>
>> 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.
>>
>> -Chris
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
> Untracked with Trackbuster <https://trackbuster.com/?sig>_______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/1cd0d176/attachment.html>
More information about the swift-evolution
mailing list