Skip to content

Improve Error Reporting: Show better error when using ref cell instead of not #1276

@isaacabraham

Description

@isaacabraham

What

The following code snippet leads to the resulting error message: -

let x = 5
if !x then "foo" else "bar"

This expression was expected to have type bool ref but here has type int

Why

The error is likely to occur for newcomers to the language who expect it to provide negation behaviour, when F# sees it as ref cell deferefencing, hence the confusing error message. With the improved support for implicit handling of rec cells through the mutable keyword, this error could be reworded to explain that perhaps they meant not instead of !.

How

Suggest an error wording as follows: -

The ! operator is used to dereference a ref cell. Did you want to negate the expression instead? If so, consider using 'not expression' instead.

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