Forced non optional coalesce that behaves like a guard else nothing statement

What do you think about this:

let firstElement = list.first() !? return
let firstElement = list.first() !? break
let firstElement = list.first() !? continue

I know this is a common topic. It's just one more attempt at finding a shorthand for the famous guard let else empty return statement.

1 Like

What is the expected behaviour here? Or rather, what is the equivalent code in current Swift? That syntax is very confusing to me.

let firstElement = list.first() ?? "defaultValue"

let firstElement = list.first() !? return 

the second would be the same as

guard let firstElement = list.first() else {
     return
}

Abbreviating guard...else with new syntax is a commonly rejected change. This particular suggestion hasn’t been listed in the document on such changes, but it would fall under the same umbrella as the others, I would think.

On the other hand, see future directions at the end of the original post of this thread: Uninhabited Type (Never) Conversions

[EDIT] Note that the above linked thread is not an accepted direction for the language, it’s just a discussion.

Not sure I would want to be able to avoid guard statements where I’d otherwise be returning or continuing but the idea of throwing if nil coalescing fails is appealing to me.

Terms of Service

Privacy Policy

Cookie Policy