SoftTimur
SoftTimur

Reputation: 5500

Where to define common functions in Ocaml?

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!

Upvotes: 3

Views: 244

Answers (1)

Victor Nicollet
Victor Nicollet

Reputation: 24577

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!" ...

Upvotes: 5

Related Questions