15

does OCaml support infix functions defined in plaintext ?

arg1 `plus` arg2 = arg1 + arg2 

thanks

desmogix
  • 217
  • 2
  • 8

2 Answers2

14

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
Virgile
  • 9,724
  • 18
  • 42
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

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • 6
    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 Jul 01 '16 at 22:42