Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

Comments

@isaacabraham
Copy link
Contributor

@isaacabraham isaacabraham commented Apr 24, 2016

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 mentioned this issue Apr 24, 2016
37 of 71 tasks complete
@smoothdeveloper
Copy link
Contributor

@smoothdeveloper smoothdeveloper commented Apr 24, 2016

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
Copy link
Contributor Author

@isaacabraham isaacabraham commented Apr 24, 2016

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

@forki
Copy link
Contributor

@forki forki commented Apr 25, 2016

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
Copy link
Contributor

@forki forki commented Apr 25, 2016

How about this?

elmify7

@ashtonkj
Copy link

@ashtonkj ashtonkj commented Apr 25, 2016

I like where this going.

@forki
Copy link
Contributor

@forki forki commented Apr 25, 2016

WIP PR: #1115

@forki
Copy link
Contributor

@forki 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 Warning when the result of a comparison is ignored Improve error reporting: Warning when the result of a comparison is ignored Apr 26, 2016
@cartermp cartermp added this to the Unknown milestone Aug 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.