简体   繁体   中英

Ocaml syntax error: why are parentheses enclosing (if …) in function application necessary?

Entering this code to ocaml toplevel results in syntax error.

(fun n -> n + 1) if true then 1 else 2

But this code is OK.

(fun n -> n + 1) (if true then 1 else 2)

Why are parentheses necessary?

Because an expression like

f if a then b else x y

would be ambiguous (besides being hard to read).

This is a consequence of the priority table of ocaml operators priority . A function has left associativity, whereas if does not have associativity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM