Skip to content

Improve error reporting: Smarter heuristics on incorrect return type of if / else branch #1106

@isaacabraham

Description

@isaacabraham

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 123

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

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