loading...

re: Ternary Unwrapping in Swift VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Of course you can put arbitrary functions in either, so in general this operator will be useful – but when both code paths differ only in data you ...
 

Just in an attempt at playing code golf, the int example could also be written as

let sentence = optionalNumber.map { "My favorite number is \($0)" }

Though, of course again it's specific to the second code path happening to be convenient – in general the operator will remain more useful.

One could write

let sentence = optionalNumber.map { "My favorite number is \($0)" } ?? fallback()

But I think that fails in the direction of

improve[ing] the readability of the code

So your solution wins here ;-)

 

Ultimately, it's a matter of taste.
The ?? | operator, however, allows me express the same idea in the style of an operator (ternary conditional) that's already common in many programming languages.

code of conduct - report abuse