Skip to content

Improve error reporting: Warning when the result of a comparison is ignored #1109

@isaacabraham

Description

@isaacabraham

What

The following code raises a compiler warning that a boolean expression is being implicitly ignored.

let x = 10
let y = "hello"

let changeX() =
    x = 20
    y = "test"

warning FS0020: This expression should have type 'unit', but has type 'bool'. Use 'ignore' to discard the result of the expression, or 'let' to bind the result to a name.

Why

For a new developer to F#, they are most likely trying to mutate the values of x and y but do not know about <-. I have seen first hand a developer "fix" these warnings by putting |> ignore after every expression, and then wonder why their application does absolutely nothing at all.

How

The warning should be much clearer: -

The = operator in F# is used for the purposes of comparison, not assignment. Did you intend to set the value of x instead? If so, use the '<-' operator instead e.g. x <- 20.

The remainder of the error message can be the same as in #1108.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature ImprovementTheme-Simple-F#A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions