Skip to content

Improve error reporting: Missing "else" branch for if / elif expression #1157

@isaacabraham

Description

@isaacabraham

What

Related to #1104, but not the same. The following code leads to an especially cryptic error message from the compiler:

let x = 10
let y =
   if x < 10 then "test"
   elif x < 20 then "foo"

As if is an expression in F#, you need to provide an else branch as well. The error message shown below is related to the elif branch above (but not the if branch): -

error FS0001: This expression was expected to have type
    string    
but here has type
    unit

Why

What's especially confusing about this error (in addition to the points put forward in #1104) is that the error suggests that the type being returned from this branch is a unit when in fact it is a string. Even worse, if you comment out the elif branch so that you just have the if branch, the types in the error message become inverted!

How

As per #1104.

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