In another recursive routine (alpha beta algorithm) I calculate a score. Something like this:
fn search(depth: u8) i32 {
if (depth == 0) return evaluation();
if (self.check_timeout()) return 0; // if timeout self.stopped = true
while (...) {
const score = -search(depth - 1);
if (self.stopped) break;
// code
}
}
Using ?i32 as return value in case of a timeout is syntactically inconvenient.
I once saw Andrew write something like “errors are for control flow” so what about this:
In the program it saves me a whole buch of if (self.stopped) checks.
Coming from other languages it feels like “abusing exceptions” because it is not an exception but a normal state of the program.
Are we abusing here or is it just OK?
The notion of using an error to handle this kind of condition seems basically sound to me. Error just means “if this happens, we can’t keep doing what we’re doing”, a timeout is a relevant case of that pattern.