[swift-evolution] [Pre-Draft] Nil-coalescing and errors
Radosław Pietruszewski
radexpl at gmail.com
Sun Apr 10 13:33:43 CDT 2016
Just FWIW:
> ```
> 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 `|?`.
I agree. But I would also say that deserializing from JSON is complex enough of a problem to warrant a domain-specific solution. There’s a ton of them, of course, but for example, with Freddy, it would look something like:
extension Person: JSONDecodable {
init(json j: JSON) throws {
firstName = try j.string(“firstName”)
lastName = try j.string(“lastName”)
age = try j.int(“age”)
}
}
let person = try Person(json: json)
Even with the proposed `???`, a domain-specific solution would be arguably better. So I just don’t consider that a compelling use case.
— Radek
> On 09 Apr 2016, at 16:56, Yuta Koshizawa via swift-evolution <swift-evolution at swift.org> wrote:
>
>> 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
> _______________________________________________
> 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/20160410/cd01c637/attachment.html>
More information about the swift-evolution
mailing list