3

I have some very basic and simple functions shared by several .ml files: for instance, warn, error... I would like to know, instead of repeating their definition in each .ml file, how to define them in a common place, and just call them when necessary? Is it necessarily a module?

Thank you very much!

1 Answer 1

5

Every file in OCaml defines a module. For instance, you could place your common definitions in:

(* common.ml *)
let error msg = ...
let warn  msg = ...

And then use it from other files as such:

... Common.error "Naughty event!" ...

Or as such:

open Common

... error "Naughty event!" ...
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much... Another question is if this .ml file needs to be compiled to be opened/used in other files, which types of file are actually required: .ml, .mli, .cmi, .cmx, .o?
You need .cmi and either .cmx or .cmo depending on whether you're using ocamlopt or ocamlc (respectively).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.