Return early and continue on the other path
A guard clause handles a special case before the main computation:
def clamp_low(value: i64, minimum: i64) -> i64:
if value < minimum:
return minimum
value
def main() -> Result[(), IoError]:
print(clamp_low(3, 10))?
print(clamp_low(12, 10))?
Ok(())
10
12
return exits the entire function, even when it appears in nested branches.
Every return must match the function’s declared result. Code after a guaranteed
exit is rejected. Every path that reaches the end must also supply the declared
result; handling just one branch is not enough:
def incomplete(flag: bool) -> i64:
if flag:
return 42
def main() -> ():
pass
expected i64, got ()