0

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

Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57

1 Answers1

1

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.

Guillaume Melquiond
  • 1,243
  • 4
  • 11
  • Wouldn't it be possible to create a module or object as a base class that wraps around js_of_ocaml objects instead? To get rid of the heave syntax. Or maybe just alias `Js.Unsafe.get` to `get` locally? – Olle Härstedt May 25 '20 at 08:13
  • The syntactic sugar is only there to ensure that expressions are correctly typed. If you do not care about typing you can indeed directly call `Js.Unsafe.get` or whatever alias you choose. – Guillaume Melquiond May 25 '20 at 08:38
  • OK, but if it also checks the type it's not just a syntactic sugar...? – Olle Härstedt May 25 '20 at 08:59
  • It is just syntactic sugar, but the generated OCaml code contains a lot of type casts `e : t`. The compiler is thus forced to check the types. – Guillaume Melquiond May 25 '20 at 09:21
  • Hm, there's no manual page that extends the syntax, to demonstrate how it works? Like a "before and after" comparison. – Olle Härstedt May 25 '20 at 13:22
  • There are some comparisons in the comments of the source file: https://github.com/ocsigen/js_of_ocaml/blob/a8a41bc57f130e853a7398a2d4af1a885e2c1b7b/ppx/ppx_js/lib_internal/ppx_js_internal.ml#L322 – Guillaume Melquiond May 25 '20 at 13:48