[swift-evolution] [Pre-Draft] Nil-coalescing and errors

Thorsten Seitz tseitz42 at icloud.com
Sat Apr 16 06:24:44 CDT 2016


> Am 12.04.2016 um 01:01 schrieb Yuta Koshizawa <koher at koherent.org>:
> 
> Hi.
> 
> Decoding a JSON is just an example. As I replied to Thorsten, we can
> think about various cases we want to unwrap multiple optionals at
> once.
> 
> This is another example (with the notation `|?` and the postfix
> version I proposed instead of the infix `???`).
> 
> ```
> do {
>  let sum = try Int(aString)|? + Int(bString)|?
> } catch _ {
>  // Error handling
> }
> ```
> 
> With optional binding, we need to write something like the following.
> It needs additional assignments (bindings) to `a` and `b`.
> 
> ```
> if let a = Int(aString), b = Int(bString) {
>  let sum = a + b
> } else {
>  // Error handling
> }
> ```

Actually I find this much more readable than the version above which mixes error handling (|?) and the business logic (a + b), making the business logic more difficult to understand. Using meaningful names instead of `a` and `b` will increase readability even more.

-Thorsten



More information about the swift-evolution mailing list