Add nil-collapsing call expression

This topic was discussed just last week by core team members: Unwrap or Throw - Make the Safe Choice Easier - #3 by Joe_Groff.

This idiom appears nice on the surface, but it doesn't interact well with the use case where the function itself is throwing. The try? myFunc(optionalArg ?? throw MyError()) idiom swallows errors thrown by myFunc in addition to those thrown when a nil argument is encountered. Proper support requires a feature that is orthogonal to the error system, just like optional chaining is orthogonal.

FWIW, the general structure underlying a solution to this problem is Applicative. Some programming languages include syntactic sugar called "idiom brackets" which addresses the use case for optionals, but also provides sugar for other Applicative types such as Result, Future, etc. I'm not sure exactly what this kind of syntactic sugar would look like in Swift but I would love to see it explored and added to the language someday.

1 Like