OCaml: Why I can't use this operator infix?

Deadly 提交于 2019-11-30 03:05:58

In OCaml, whether an operator is infix or prefix is determined by its first character. In you case, the character '~' is for prefix: by let (~=~) a b = ..., you are defining a prefix operator. ~=~ a is a valid expression, and returns a function.

In addition to infix or prefix, infix operator associativity (left or right) and operator precedences (which of + and * has stronger?) are syntactically determined by the first character of the operator.

This sounds ugly, since you cannot have control of your fancy operators characteristics, but it makes easier to read OCaml source code by someone else with lots of strange custom operators.

Here is the table of chars for operators:

The first char   :  prefix/infix/connectivity power/left-or-right
! ~ ?            :  prefix
= < > | & $      :  infix0, left
@ ^              :  infix1, right
+ -              :  infix2, left
* /              :  infix3, left  ( ** is exceptional. It is right assoc and have power 4)

By lexical conventions of ocaml ~ is reserved for prefix operators, see http://caml.inria.fr/pub/docs/manual-ocaml/lex.html#infix-symbol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!