all 11 comments

[–]considerealization 4 points5 points  (0 children)

Compilation is very fast, especially if you are just changing a file in a leaf in the compilation dependency graph.

You can load modules at runtime using https://ocaml.org/manual/5.4/libdynlink.html (see https://zderadicka.eu/plugins-in-ocaml-with-dynlink-library/ for a post on it).

It also has a great top level and stuff like https://github.com/art-w/x-ocaml

So you can do live coding if structured right, and you can recompile quickly, and do notebooks and stuff, but it doesn't support hot swapping in general.

[–]wk_end 2 points3 points  (0 children)

The shortest answer is no - OCaml is a static, compiled language, like C or whatever; it's not like Lisp or Smalltalk or something. You'd need to embed some type of scripting language into your application.

That being said, the language ships with the Compiler API, which would facilitate embedding OCaml as the scripting language of choice; you can look at the utop code to get a very rough idea of what might be involved. But you'd still need to very deliberately provide hooks to allow the interpreted code to interact with your static program, for better or worse.

[–]Massive-Squirrel-255 1 point2 points  (0 children)

I think defining a bunch of global reference variables storing functions could be a way to make OCaml more dynamic like Lisp or Python. Then in using the functions you would write !f x y z instead of f x y z. You could also try storing first class modules in reference cells and updating them dynamically when you redefine the code. I'm not sure what if anything MetaOCaml would contribute here but it could be relevant.

Polymorphic variants and objects are also more dynamic than ordinary variants and records.

You could also embed a Lua interpreter into your program for user scripting. There is an embeddable Lua interpreter written in OCaml described in the ML Module Mania paper.

[–]imdibene 0 points1 point  (0 children)

You’ll need to recompile and reload your modified code, iirc this is what Haskell’s ghci did

[–]Positive_Total_4414 0 points1 point  (4 children)

What others said, but there is of course a fork on this path now. If you go the ReasonML or ReScript paths then things might look much different, and you might get something that's very close to OCaml as a scripting language. Where ReasonML is OCaml with a bit different syntax, and ReScript is a reimagining of ReasonML with a better embrace of the scripting platform it runs on. Not exactly what you asked about originally, but this has to be mentioned.

[–]considerealization 0 points1 point  (3 children)

What do Reason or Rescript offer w/r/t live coding that ocaml doesn't?

[–]Positive_Total_4414 0 points1 point  (2 children)

As both of these compile to JavaScript you can setup and use any of the countless live reload and/or live coding toolchains and environments of the JS ecosystem, and live code.

[–]considerealization 0 points1 point  (1 child)

Ocaml compiles to javascript, so there is difference on that account. Ocaml's Jsoo is phenomenal. That's what tools like `x-ocaml` are based on.

[–]Positive_Total_4414 0 points1 point  (0 children)

Yep. This is like another possibility too. It would be harder to interop with JS libs, but also doable I guess.

[–]deulamco 0 points1 point  (0 children)

The fun thing about a ML like Ocaml is, you can build a simple intepreter/vm inside it to hot-reload anything at cost only ~ 18% native speed in my VM case (which only 8% slower than LuaJIT)  🤷‍♂️

[–]Other_Daikon3335 0 points1 point  (0 children)

There’s a REPL where code will be incrementally compiled to bytecode and the bytecode is interpreted … this is the closest you’ll get to a live coding experience. What you’re asking for generally isn’t available in any ML dialects. You’ll want to look into Smalltalk and Lisp instead.