[swift-evolution] Optional Argument Chaining

Pertti Kröger pertti.kroger at qvik.fi
Thu Dec 14 02:25:33 CST 2017


I do not feel new syntax is needed for this.
You can do this currently:

postfix operator ¿

extension Optional {
    struct UnwrapError: Error {}
    static postfix func ¿(_ value: Optional) throws -> Wrapped {
        if let value = value {
            return value
        } else {
            throw UnwrapError()
        }
    }
}

Given function f: (Foo, Foo, Foo) -> Void
and functions a, b, c: () -> Foo?
you would then chain the calls like

try? f(a()¿, b()¿, c()¿)

This is hardly more verbose than

f(a()?, b()?, c()?)

At least on mac keyboard ¿ is not hard to type either.
Also everyone is free to bikeshed the operator to their liking.

If special syntax were to be added for this, then in my opinion
the behaviour should mimic this; i.e. evaluate arguments left-to-right,
abort on first nil.

 -Pertti
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171214/86884961/attachment.html>


More information about the swift-evolution mailing list