Worice
Worice

Reputation: 4037

Operators precedence and association

I defined a XOR operator:

let (.||.) x y = (x || y) && not(x && y)

Such that

true .||. true
true .||. false

do return false and true respectively.

According to Hansen & Rischel, the && operator has higher precedence over the || operator (and the .||. operator too. Hence, why

    true .||. true && false
    true .||. false && true

do return false and true respectively? The results appears to be produced by (true .||. true) && false instead of the expected true .||. (true && false).

Upvotes: 2

Views: 131

Answers (1)

Fyodor Soikin
Fyodor Soikin

Reputation: 80744

According to MSDN, the operator .||. would fall under the pattern |op (i.e. ignoring leading dot, starting with pipe), which is two lines below operator && in the table, on the same line with &op and <op among others. So it actually has higher precedence than &&.

The F# spec says the same thing in section 4.4.2, only the table is upside down there (highest to lowest).

Can't comment on the book you're reading, don't have it handy.

Upvotes: 7

Related Questions