ocamljs-of-ocaml

Why is the ##. operator needed in js_of_ocaml?


Not sure why all those operators are needed. What's the rationale? Why is not the regular OCaml object syntax enough?

obj##.m
obj##.m := e
obj##m

Documentation here: http://ocsigen.org/js_of_ocaml/3.6.0/manual/ppx


Solution

  • OCaml objects do not have properties. If you write obj#m, you are calling method m on object obj. If you write obj#m := e, you are again calling method m on object obj and it returns a value of type 'e ref, which is then passed to operator (:=).

    Hence operator ##., which is just syntactic sugar for calling Js.Unsafe.get, respectively Js.Unsafe.set. (Similarly, obj##m x y is syntactic sugar for Js.Unsafe.meth_call obj "m" [|x; y|].)

    Rather than modifying the OCaml compiler in depth to actually map Javascript objects to OCaml ones and correctly recognize getters/setters, JSOO is a thin layer that depends on OCaml objects only for typing Javascript ones and ignores them entirely for execution.