Before this PR, return statements were not allowed in conditional statements whatsoever. This is because return statements dont actually return from a function. Code after a return statement in a function is always executed. The return statement is just the only statement allowed to push data onto the stack. So, as long as no function returns more than once, and each function always returns exactly what their return type is, there are no issues.
To prevent functions from accidentally not returning, or accidentally returning too much, I typechecked against returns in any kind of conditional statement or loop. Now, return statements are allowed in if-else expressions, as long as both branches return a value. These if-else return statements can also be nested, like so:
fn test(n: num) -> num {
if n < 0 {
return 1;
} else {
if n < 10 {
return 2;
} else {
return 3;
}
}
}
This PR adds typechecks against single branch ifs with a return statement, if-else expressions with only a single branch that returns, and loops that return.
Before this PR, return statements were not allowed in conditional statements whatsoever. This is because return statements dont actually return from a function. Code after a return statement in a function is always executed. The return statement is just the only statement allowed to push data onto the stack. So, as long as no function returns more than once, and each function always returns exactly what their return type is, there are no issues.
To prevent functions from accidentally not returning, or accidentally returning too much, I typechecked against returns in any kind of conditional statement or loop. Now, return statements are allowed in
if-else
expressions, as long as both branches return a value. Theseif-else
return statements can also be nested, like so:This PR adds typechecks against single branch ifs with a return statement, if-else expressions with only a single branch that returns, and loops that return.