dotnet / dotnet/fsharp

Using '&&' can lead to hard-to-understand errors

Open
#5,061 1 comment 0 reactions 0 assignees View on GitHub
Area-Diagnostics Feature Improvement
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Given the following code:

```f#
module TestMe =
let tryMeOut x y =
x = 10 && y = 12 |> Some
```

I received the following error:

![image](https://user-images.githubusercontent.com/16015770/40816259-37326770-654c-11e8-985e-4f95631b5cf5.png)

Now, even though the original line was slightly more complicated than this example, I must admit I felt quite ashamed that I totally didn't understand what was going on. What does the compiler mean with _"expecting a bool -> bool"_? There's nothing to expect here, I give it a _"bool -> bool option"_ for a reason, right?

It took me longer than I care to admit to realize that for some reason, `&&` has lower precedence than `|>` leading this expression to be interpreted as (I think):

```f#
module TestMe =
let tryMeOut x y =
(x = 10 )
&&
(y = 12 |> Some)
```

I'm not 100% sure how this could be improved, perhaps someone has a better idea for an error, or having the compiler point to another part of the code, but one thing *is* clear to me, even after years of programming in F#, every now and than I am still stymied by cryptic messages like these.

(and while I'd love to suggest a different order of precedence, that ship must have sailed many years back)

(btw, [the docs arent't very helpful](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/), even had I thought of looking there first: it lists `&&` twice, both before and after `|`, and `|>` is missing in the precedence list)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.