Running OCaml scripts

You may be right. I typed that from memory. Adding -S should do it:

#!/usr/bin/env -S opam exec --switch machine -- ocaml
2 Likes

Oh TIL (or rather, totally forgot that one exists).
Doesn’t seem to be portable though, it doesn’t work on my Alpine : (

What doesn’t work ? Alpine’s env doesn’t support the option ?

There’s also BINFMT_MISC.

env -S is recent enough that CentOS 7 doesn’t have it, so older enterprise systems won’t have it.

There’s also ocamlscript in opam:

#! /usr/bin/env ocamlscript
let () = print_endline "Hello, world!"

Scripts have many institutional advantages: they’re transparent, people can look at them and see what they do, or pick up on casual documentation that wouldn’t make its way to --help, people can easily troubleshoot into code with just the file on hand and not needing special access. Since it’s implied that the compiler’s also around people can take troubleshooting as far as seeing if a small edit will fix things, and you don’t have worry about tedious things like binary glibc incompatibility when deploying updates to scripts.

There are also disadvantages, of course.

The trend in other languages is to have single-file packages that fit something like a dune configuration into a special section of your code: rust-script, d’s dub, nimcr.