[swift-evolution] throw expressions in ternary operators

Krzysztof Siejkowski krzysztof at siejkowski.net
Mon May 2 08:41:01 CDT 2016

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. `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 :)



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/8911a525/attachment.html>

More information about the swift-evolution mailing list