-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error reporting: if / else branches return different types. #1105
Comments
Isn't it more a matter of Paraphrased from here. |
Could be more than two branches. |
True. How about: |
or: |
Hmmm... does it matter whether the branch is an if / elif / else branch? They're all branches of the same expression, they should all match. |
@isaacabraham, I guess it does make sense not to split the "then" and "else" branches (as you mentioned here too). I still think that mentioning the existence of these branches could be helpful for newcomers. I tried adopting @smoothdeveloper 's way of listing the types in the following suggestion: |
Don't understand. I'm not sure where I mentioned separating else / then branches - if I did I shouldn't have, I don't think it's a good idea. But that's IMHO. |
@isaacabraham, I'll try to be clear: I first suggested to split the branches. You said that the branches shouldn't be split. I agreed with you. We are now on the same page. The only point I was/am still making is that just saying "branches" might not make a lot of sense to new F# programmers. That's why I thought it could be clearer to write something like 'all "then" and "else" branches' (which is just a longer and clearer way of saying 'all branches'). The actual list of inconsistent types should not be split. This is exactly what I suggested: |
BTW, the same issue can be obtained with seq/array/list expressions, function/match expressions, I think they should all be addressed in same fashion. |
resolved by #1142
If you feel there is still more work needed then pleas submit some additional suggestions and or a follow up PR. |
What
The following code results in an error message which could be improved upon: -
The error is: -
error FS0001: This expression was expected to have type string but here has type int
Why
Newcomers to expression-based conditionals often do not understand that all branches of a conditional must evaluate to the same type of result. The current error message identifies that two types do not match, but does not make clear the context in which this is an error.
How
A better error might be:
All branches of an if / else expression must return the same type. The other values in this if / else expression all return a value of type
string.
If possible, the values of the other branches should be included in the error message (of course, this will not always be possible).
The text was updated successfully, but these errors were encountered: