OCaml Syntax Sucks

By Xah Lee. Date: . Last updated: .

OCaml's let = in is very confusing. When nested, you can't tell how it's nested.

let wafin fd ?pos ?len buf = let loc = "wafin" in let pos = gop ~loc pos in let len = gol buf ~pos len in csa ~loc buf ~pos ~len; uw fd buf ~pos ~len ;;

another problem is that you have global and local declaration with the same syntax, but no bracket to indicate scope. Example:

another problem is that function definition also uses the same syntax. Example:

though the last point is consistent, because a 0 arity function without side-effect is just constant.

but the effect of all these is that in a ocaml source code you'll have nested let of the above. Quite confusing to read.

MetaLanguage has an “end”

In OCaml's parent language ML, it has a “end”, like this:

let = in end

This is better in the sense that it more clearly indicates the begin and end points of the expression.

Not really better in practice because now it's more typing.

The advantage of clearer indication of nesting doesn't really apply, because in practice, the code is broken down to lines, with proper indentation.

see also Nested Syntax: Brackets vs Begin/End vs Indentation

History of OCaml Syntax, and The Next 700 Programming Languages

ocaml haskell ISWIM

Here is the lineage of OCaml's syntax:

Note that OCaml syntax came from ML, and ML syntax came from a article describing an ideal language called ISWIM, described in the paper:

History of OCaml and Haskell Syntax, and The Next 700 Programming Languages

What Could be Better?

So, if we stick with OCaml/ML's style of syntax, what could be better for the “let” syntax?

That's a hard question, because ML syntax is really a odd ball. It somewhat wanted to be like English.

Then, we trace its history, indeed, in ISWIM, it seems to be one man's whim where it espouses a notation that's half English, half math notation, and intended to be the syntax for the next seven hundred languages.

Facebook Fix to OCaml Syntax

If we do not stick to OCaml style of syntax, then, there's 2 obvious possibilities. One is going with lisp. Another, is going with C-syntax.

Facebook created Reason, which is OCaml in JavaScript syntax. https://reasonml.github.io/

See also: OCaml Tutorial

Systematic Grammar