Skip to content
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

Closed
Tracked by #1103
isaacabraham opened this issue Apr 24, 2016 · 11 comments
Closed
Tracked by #1103

Comments

@isaacabraham
Copy link
Contributor

isaacabraham commented Apr 24, 2016

What

The following code results in an error message which could be improved upon: -

let y =
    if test > 10 then "test"
    else 123

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).

@lulu-berlin
Copy link

Isn't it more a matter of if ... then ... else ...?
How about something like The types of the values produced in the "then" branch (string) and the "else" branch (int) of this conditional expression do not match.

Paraphrased from here.

@isaacabraham
Copy link
Contributor Author

Could be more than two branches.

@lulu-berlin
Copy link

True. How about: The types of the values produced in all "then" (string) and "else" (int) branches of a conditional expression must match ?
With more branches, it could be: The types of the values produced in all "then" (string, int, int) and "else" (int) branches of a conditional expression must match.

@lulu-berlin
Copy link

or: are expected to match

@isaacabraham
Copy link
Contributor Author

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.

@lulu-berlin
Copy link

@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:
The values produced in all "then" and "else" branches of a conditional expression are expected to have the same type but here have inconsistent types: string 1 occurrence, int 1 occurrence. Make expression type consistent across all branches

@isaacabraham
Copy link
Contributor Author

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.

@lulu-berlin
Copy link

lulu-berlin commented Apr 26, 2016

@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:
The values produced in all "then" and "else" branches of a conditional expression are expected to have the same type but here have inconsistent types: string 1 occurrence, int 1 occurrence. Make expression type consistent across all branches

@smoothdeveloper
Copy link
Contributor

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.

@forki
Copy link
Contributor

forki commented May 2, 2016

Will do this together with #1104 in #1142

@KevinRansom
Copy link
Member

@isaacabraham @forki

resolved by #1142

> let test = 6
- let y =
-     if test > 10 then "test"
-     else 123;;

      else 123;;
  ---------^^^

stdin(18,10): error FS0001: All branches of an 'if' expression must return the same type. This 
expression was expected to have type 'string' but here has type 'int'.
>

If you feel there is still more work needed then pleas submit some additional suggestions and or a follow up PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants