[swift-evolution] throw expressions in ternary operators

Chris Lattner clattner at apple.com
Mon May 2 16:30:06 CDT 2016


> On May 2, 2016, at 6:41 AM, Krzysztof Siejkowski via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> Correct, it is not ternary, just a slip of my mind :)
> 
> I have a great amount of understanding for that, I find `nil coalescing operator` name simply impossible to remember :)
> 
> 
> 
>> I didn’t know this workaround worked. Cool! Can somebody from the core team tell us if it not supporting throw directly is a bug or an intended feature?
> 
> Just to elaborate a little after I thought about it for a minute: intended feature. The core reason is that `throw` is a statement (the same way as `if` or `guard` are), and in Swift statements are not expressions.
> 
Yep, Krzysztof is exactly right here.  It would be possible to turn “throw” into an expression - this is precedented in other languages (like C++ iirc).

-Chris



> `try`, on the other hand, is an expression.
> 
> So you cannot simply `throw` the same way you cannot write:
> 
> let elem : AnyObject = “42"
> let int = elem as? Int ?? if true { return 42 }
> but you can write:
> 
> let elem : AnyObject = “42”
> 
> let int = elem as? Int ?? { if true { return 42 } }()
> 
> There has been already a few discussions whether statements should be expressions in swift or not on the list, which I’ve been only partially following, so I’m not sure if there’re any plans for changing the current statement/expression tradeoff is Swift :)
> 
> Cheers!
> 
> Krzysztof
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/17c78c2f/attachment.html>


More information about the swift-evolution mailing list