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

Created on 1 Jun 2018  路  1Comment  路  Source: dotnet/fsharp

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, even had I thought of looking there first: it lists && twice, both before and after |, and |> is missing in the precedence list)

Area-Compiler Feature Improvement

Most helpful comment

I think what might help is that the compiler somewhere prints the parenthesised version of the expression it has trouble with. In this scenario:

[...] does not match the type 'bool option' in expression
(x = 10 )  &&  (y = 12 |> Some)
---------------^^^^^^^^^^^^

>All comments

I think what might help is that the compiler somewhere prints the parenthesised version of the expression it has trouble with. In this scenario:

[...] does not match the type 'bool option' in expression
(x = 10 )  &&  (y = 12 |> Some)
---------------^^^^^^^^^^^^
Was this page helpful?
0 / 5 - 0 ratings