17

does OCaml support infix functions defined in plaintext ?

arg1 `plus` arg2 = arg1 + arg2 

thanks

2 Answers 2

16

No. As explained in the OCaml manual, infix operators can only contain special characters, namely !, $, %, &, *, +, -, ., /, :, <, =, >, ?, @, ^, |, ~ (or # but only in first position) and must not start with !, ? or ~.

In order to define an infix operation, you must put the symbol into parentheses:

# let (+++) x y = x + 2 * y;;
...
# 3 +++ 4;;

- : int = 11
1

The base language doesn't support infix operators that are identifiers.

You can use ppx to make more or less arbitrary extensions to the syntax of OCaml, but this isn't something to be done lightly (IMHO).

Here is a page with links to ppx info: https://ocaml.io/w/PPX

1
  • 8
    it does have the identifiers land, lor, lxor, lnot, lsl, lsr, asr, mod, and or as infix operators, but you can't define your own
    – newacct
    Commented Jul 1, 2016 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.