-
Notifications
You must be signed in to change notification settings - Fork 833
Description
What
If types are incompatible across all branches of an if / else expression, the return type of the first branch is used to determine the "correct" type. Given the following code sample, this behaviour, whilst consistent, is usually not what the user expects: -
let test = 10
let y =
if test > 10 then "test"
elif test > 20 then 456
elif test > 30 then 789
elif test > 35 then 101010
else 123error FS0001: This expression was expected to have type string but here has type int
In this case, all four branches that return an integer are flagged as having an error when it is much more likely that the first branch ("test") is in error.
Why
Changing the first type in a conditional causes all the other cases to break, when in fact it's normally the first one that's in error. A beginner will follow all the error messages raised by the compiler and not realise that they are actually all correct, but in fact the error is located in the one branch that the compiler has not flagged.
How
The compiler should ideally find the most "common" return type and flag that as the correct one and mark the other branch(es) as being in error.