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

Comments

Projects
None yet
6 participants
@isaacabraham
Contributor

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 referenced this issue Apr 24, 2016

Open

Improve error reporting #1103

35 of 64 tasks complete
@smoothdeveloper

This comment has been minimized.

Show comment
Hide comment
@smoothdeveloper

smoothdeveloper Apr 24, 2016

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@isaacabraham

isaacabraham Apr 24, 2016

Contributor

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

Contributor

isaacabraham commented Apr 24, 2016

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

@forki

This comment has been minimized.

Show comment
Hide comment
@forki

forki Apr 25, 2016

Contributor

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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@forki

forki Apr 25, 2016

Contributor

How about this?

elmify7

Contributor

forki commented Apr 25, 2016

How about this?

elmify7

@ashtonkj

This comment has been minimized.

Show comment
Hide comment
@ashtonkj

ashtonkj Apr 25, 2016

I like where this going.

ashtonkj commented Apr 25, 2016

I like where this going.

@forki

This comment has been minimized.

Show comment
Hide comment
@forki

forki Apr 25, 2016

Contributor

WIP PR: #1115

Contributor

forki commented Apr 25, 2016

WIP PR: #1115

@forki

This comment has been minimized.

Show comment
Hide comment
@forki

forki Apr 26, 2016

Contributor

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/

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

@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