Precedence of custom operators in F#

Viewed 62

I'm trying to create operators similar to (&&) and (||), and with the same precedence.

My code is:

let (&&.) = (&&)
let (||.) = (||)
printf $"{true ||. true &&. false} = {true || true && false}"

This prints "False = True".

As I understand it, in F# the precedence of custom operators is defined by their first characters. Why do I get this output, and how do I fix it?

1 Answers

I think this is working as intended. According to this table && has a higher precedence than ||, but &&. and ||. have the same precedence (as instances of &op and |op).

To fix it, you could choose an operator with higher precedence for your custom "and" function, like this:

let (^^.) = (&&)
let (||.) = (||)
printf $"{true ||. true ^^. false} = {true || true && false}"   // "true = true"

enter image description here

Related