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

Open
isaacabraham opened this Issue Apr 24, 2016 · 7 comments

Projects

None yet

5 participants

@isaacabraham
Contributor
isaacabraham commented Apr 24, 2016 edited

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.

@isaacabraham isaacabraham referenced this issue Apr 24, 2016
Open

Improve error reporting #1103

27 of 48 tasks complete
@smoothdeveloper
Contributor

equal operator is used for comparison, if you intended to assign 20 to x, use the '<-' operator instead e.g. x <- 20

Then the developer will get a compile error about x being immutable:

This value is not mutable.

which should read

The value 'x' is not mutable, please check that it is declared as mutable: 'let mutable x = 10' if you intend to mutate it.

@isaacabraham
Contributor

@smoothdeveloper Create another issue for mutable error message and let's reference that from #1103

@forki
Contributor
forki commented Apr 25, 2016 edited

I just looked at this one. there is already a differentiation if the left side is a property.

This expression should have type 'unit', but has type '{0}'. If assigning to a property use the syntax 'obj.Prop <- expr'.

I think this will be easy to "fix". I will take this and turn it into a blog post ;-)

In other words: calling dibs

@forki
Contributor
forki commented Apr 25, 2016

How about this?

elmify7

@ashtonkj

I like where this going.

@forki
Contributor
forki commented Apr 25, 2016

WIP PR: #1115

@forki
Contributor
forki commented Apr 26, 2016

I also wrote a small tutorial for potential newconers based on that problem:http://www.navision-blog.de/blog/2016/04/25/make-failure-great-again-a-small-journey-into-the-f-compiler/

@isaacabraham isaacabraham changed the title from Warning when the result of a comparison is ignored to Improve error reporting: Warning when the result of a comparison is ignored Apr 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment