How to define an infix (not symbolic aka not an operator) function in OCaml?
Asked Answered
U

2

17

does OCaml support infix functions defined in plaintext ?

arg1 `plus` arg2 = arg1 + arg2 

thanks

Unmerciful answered 1/7, 2016 at 14:22 Comment(0)
B
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
Buddha answered 1/7, 2016 at 14:59 Comment(0)
D
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

Detonate answered 1/7, 2016 at 15:1 Comment(1)
it does have the identifiers land, lor, lxor, lnot, lsl, lsr, asr, mod, and or as infix operators, but you can't define your ownDepilate

© 2022 - 2024 — McMap. All rights reserved.