0

Is it possible to cast or coerce an OCaml object into a js_of_ocaml object and back?

Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
  • What is your use case exactly? `Obj.magic` is a simple way to cast between an OCaml object and a JSOO one, but whether it will work in a meaningful way depends on your use case. – Guillaume Melquiond May 25 '20 at 06:56

1 Answers1

0

OCaml object and JS object do not have much in common. They are represented and executed very differently by js_of_ocaml.

There is no way to easily translate from one to the other (just like there is no easy way to turn a record into an object, those are two things that do not have much in common).

Drup
  • 3,679
  • 13
  • 14
  • But an OCaml object is pretty similar to a JavaScript object, in most use-cases, no? – Olle Härstedt May 25 '20 at 12:57
  • Just like a record is "pretty similar" to an OCaml object. It's a bunch of fields trown together. :) In practice however, they are implemented very differently, their semantics and typing is different, etc. It's the same for JavaScript object. – Drup May 25 '20 at 16:17
  • Could the implementation be made cleaner if it was decided to support only a subset of JavaScript? – Olle Härstedt May 25 '20 at 17:34
  • I don't understand your question. JSOO supports what is needed to bind again JS libraries (and nothing more). What are you trying to achieve exactly for all this to matter ? – Drup May 25 '20 at 20:12
  • Hm, mostly trying to understand the rationale behind the design, comparing to ReasonML and its interop system, etc. – Olle Härstedt May 25 '20 at 21:03
  • Also, syntax like `##.` begs the question, why is this necessary? ;) The docs does not go into detail regarding this. – Olle Härstedt May 25 '20 at 21:04
  • Bucklescript (ReasonML is the syntax) decided to modify the semantics for accebility by, among other things, compiling OCaml object like Javascript object. This makes interop easier, at the price of changing the semantics (and breaking programs). JSOO make the choice of being fully compatible with OCaml, which means separating the two constructions. – Drup May 26 '20 at 08:37
  • Some details about here: https://bucklescript.github.io/docs/en/object-2 – Olle Härstedt May 26 '20 at 11:00