[Pitch] Allow alternate version of 'Guard' with 'Guards'

Proposing an alternate of guard named guards that implicitly returns out of the function and would supply nil if an optional return was declared. This would be purely for the sake of brevity yet still allow for the more explicit guard version to be used.

example:
guard requiredObject != nil else { return }
or
guard requiredObject != nil else { return nil }

becomes:
guards requiredObject != nil

but still allows for:
guard requiredObject != nil else {
// track analytic event or log case
return nil
}

As nice as it is to remove boilerplate, this somewhat hides the control-flow from view, making it somewhat more subtle to follow.

It also seems very special-cased to failable functions that return optionals. What’s the default return value for non-optional?

6 Likes

Changes to guard (or variations on it) were once a frequently mentioned topic. They’ve not been successful, and with the very high bar now for additions to syntax, it is unlikely to meet with success in the future.

If this family of ideas has not already been added to the list of “frequently rejected changes,” then it should be for future reference.

1 Like