er0
er0

Reputation: 1834

How do you determine the version of OCaml or Jane Street Core library at runtime

I am migrating to a recent version of OCaml (and Jane Street's Core modules), but would like my code to continue to compile with older versions. To do so, I would like to create a module with portable functions that map to either the old or new interfaces depending on the version. Querying both the version of OCaml and the version of Jane Street Core would work.

How can we look up the version of the compiler or of Jane Street Core at runtime? I know that I could pass the output of ocamlopt --version via my build system, but ideally I should not have to invoke my programs in a special way.

Upvotes: 1

Views: 485

Answers (3)

octachron
octachron

Reputation: 18892

Sorry to answer a slightly different question, but that might be a XY problem: if you want to build a compatibility package, determining the version of packages at runtime doesn't really help because something like

let f = if Sys.ocaml_version > ... then new_function else compat_function 

requires new_function to exist independently of the runtime value of Sys.ocaml_version.

A common solution for compatibility package is to detect version earlier at build time with cppo for instance or using some code generation, see for instance https://github.com/janestreet/result or https://github.com/ocaml/stdlib-shims.

Upvotes: 4

rkj
rkj

Reputation: 349

You could use Config.version from Compiler_libs as documented here https://ocaml.org/api/compilerlibref/Config.html.

Note this warning from Compiler_libs:

This library is part of the internal OCaml compiler API, and is not the language standard library. There are no compatibility guarantees between releases, so code written against these modules must be willing to depend on specific OCaml compiler versions.

Upvotes: 2

Chris
Chris

Reputation: 36451

You can retrieve the OCaml version number with Sys.ocaml_version.

utop # Sys.ocaml_version;;
- : string = "4.13.1"

I'm afraid after browsing the Jane Street Base and Core API docs, I don't see a way to query the version number at runtime.

Upvotes: 1

Related Questions