Can rescript and ocaml share any code?

Suppose I write a project where the UI is written in Rescript and React and the backend API is written in Ocaml. Is it possible for the two to share any code? It will be cool to be able to share (1) data types (2) data validations and (3) Json SerDE code between the rescript and ocaml code bases.

By share I mean that I write the code only once and then rescript use it by compiling it to JavaScript and Ocaml compiles the same code on server side into binary. but I write this code only once either in rescript or in ocaml.

I think this is possible in other environments like Scala and ScalaJS. Has anyone tried it? was it really successful in saving cost/time of project? or is it better to just develop UI and API totally independently with no code shared. (Most projects where I have worked have worked like this. UI in JavaScript and Typescript and API in Java/Scala with nothing shared).

Assuming that the shared code is written in OCaml it should be fine. I don’t know if it’s endorsed, nevertheless it’s unlikely that ReScript would drop support for OCaml syntax in the forseeable future given that it’s written in it.

There’s an example here: dream/example/w-fullstack-rescript at master · aantron/dream · GitHub

FWIW, I vaguely recall there being a topic on the subject rather recently.

3 Likes

In addition to the dream example lessp posted, here are a couple of other posts that may be of interest.

3 Likes

we do it at work, sharing some types and data serialization/parsing/validation with atdgen. It works quite well.

3 Likes

@tsnobip do you have any resources to share so that even I can get started with this? I have installed ocaml on my machine (utop, dune, etc) but I don’t have any guidance on how to make these two tool chains work with each other and share resources.

how were you able to get started?

It’s actually very easy, all you have to do it putting your code in a folder that both build systems point to!

1 Like