Skip to content

Improve error reporting: Warning when expression result is ignored #1108

@isaacabraham

Description

@isaacabraham

What

The following code snippet returns a warning that could be improved:

let foo a b = a + b
let x =
    foo 5 10
    20

The warning generated is: -

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

Why

A newcomer to F# will often not understand this; particularly if they come from a statement-focused language where the result of an expression can be arbitrarily discarded. Making this message clearer will not only reduce confusion but could also help new developers understand the value of expressions vs statements.

How

The warning could be improved as follows: -

The expression foo 5 10 returns an int which is currently being ignored. If you do not require this value, you should explicitly ignore it using the ignore function e.g. ignore (foo 5 10) or bind the result to a name e.g. let result = foo 5 10

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