(I haven't read far enough into the code to know that this is what they're doing, but the head matter I did read suggests as much. It's a common enough pattern, especially around eg argument validation and other sanity checks a function might perform to ensure it can do meaningful work at all.)
(I do wish HN supported an inline monospace markup, the <code> to a four-space indent's <pre>...)
if (foo) {
bail out
}
They're known as guard statements regardless of language.https://en.wikipedia.org/wiki/Guard_(computer_science)
Swift has a guard keyword, but the construct feels a little awkward given most languages do the above. It makes me do a double take.
guard !foo else {
bail out
}It's specifically designed to prevent bugs where flow control accidentally resumes from an error handler.
It's the same idea as the "every 'if' must have an 'else'" guideline in the code being discussed, except with "guard" the compiler will detect violations. It's a good thing.
A resumable exception initiated in the "then" part of an if will not go to "else" when control resumes; it will go to the next statement after the if.
One typically available restart is to ignore the condition and resume execution immediately after, as you describe. Another is to re-evaluate the form in which the condition was signaled. In that case, the conditional may well be itself re-evaluated with a different result, executing a different branch.
"Condition" is just a silly name for "exception". It does not mean "restartable exception". It's a terminology that Common Lisp copied from PL/I. At the time Common Lisp was being standardized, it was not a common programming language feature, so the naming didn't matter. In the decades since, the world went to "exception".
The word "condition" already has a clear meaning in computing, referring to a logical state ("condition control register", "conditional branch", ...). The "condition variable" synchronization primitive (which has no condition-like state!) is bad enough; we don't need to heap more meanings on those words.
Note that processor instruction sets have exceptions, precisely restartable, down to the instruction, without requiring a cooperating restart point. E.g. any code can hit a page fault: the exception handling will fix it up, making a page present at the faulting address, and then restart the instruction that faulted.