Skip to content

Improve error reporting: if / else branches return different types. #1105

@isaacabraham

Description

@isaacabraham

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions