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

Yuta Koshizawa koher at koherent.org
Sat Apr 9 09:56:06 CDT 2016


> I only wonder whether you really want to repeat Error() all over,
> possibly with `aString` etc. as argument.

What I really want is the postfix version of `???` as I wrote in my
first post in this thread.

> Besides the proposed infix `???`, I also want the postfix one which
> throws a `NilError` (something like `struct NilError: ErrorType {}`).
> It is useful to handle multiple `nil`s at once when we are not
> interested in the kind of the error.

And also

> I think `???` is too long. Instead, I propose `|?`.
> For `foo: Foo?`, `try foo|?` can be read like `Foo` or `nil`. It
> separates (`|`) nil (`?`) from the value and return `Foo`.
> I think it makes sense.

Then it becomes something like the following.

```
do {
    let foo: Foo = try foo(
        a: Int(aString)|?,
        b: Int(bString)|?,
        c: Int(cString)|?
    )
} catch _ {
    // Error handling
}
```

I think the infix version is also useful when we actually want to
specify the types of errors.

Swift provides two ways of error handling: optionals and do/try/catch.
I think it lacks a way to handle multiple optionals easily in some
cases. Someone uses applicative styles for it.

```
let foo: Foo? = curry(Foo.init) <^> Int(aString) <*> Int(bString) <*>
Int(cString)
```

But I think it is unreasonable to expect all programmers to understand
and master it. So I want the postfix `???` or `|?`.

-- Yuta


2016-04-08 23:47 GMT+09:00 Thorsten Seitz <tseitz42 at icloud.com>:
>
> Am 08.04.2016 um 11:59 schrieb Brent Royal-Gordon <brent at architechies.com>:
>
>>> I only wonder whether you really want to repeat Error() all over, possibly with `aString` etc. as argument.
>>
>> `Error()`, no. `SpimsterKitError.invalidWicketField("a"`), yes, because even if `Int.init(_:)` threw *an* error, it wouldn't throw *your* error.
>
> That's why I thought that in a real use case that logic would likely be extracted.
>
> -Thorsten


More information about the swift-evolution mailing list