Kevin Ji
Kevin Ji

Reputation: 10499

When are OCaml expressions at the top level of a file evaluated?

It seems that OCaml runs some expressions at compile-time; e.g., if I write

let (_ : int) = 1 / 0

at the top level of a file, the compiler will fail with an "Uncaught exception: Division_by_zero" error.

Does this mean that these values are computed at compile time in the binary? e.g., if I was using Core, and I wrote

open! Core
let date = Date.today ~zone:(force Time.Zone.local)

would this refer to the date at the time of compilation, or something else, like the date when the program is executed?

Upvotes: 0

Views: 97

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66803

I can't reproduce the behavior you describe. Here's what I see:

$ cat dz.ml
let (_ : int) = 1 / 0
$ ocamlc -c dz.ml
$ ocamlopt -c dz.ml

The two OCaml compilers are ocamlc and ocamlopt, and neither of them shows this behavior on my system.

It would help if you give a specific sequence of commands to reproduce what you observe.

Upvotes: 1

Related Questions