Declarations and global reference variables for multiple files

My folder contains several files that are compiled in the following order: global.ml, zone.ml, abs.ml,main.ml

global.mlcontains some reference variables (e.g. let g1 = ref 0) for all files.

There zone.mlis an announcement let f = !g1.

In abs.mlthere g1 := 5, which will be launched mainat the beginning of the execution, I consider it to be initialization g1, given the real context of the runtime.

Will maincall later Zone.f. Curiously, I understand what f = 5is required instead f = 0.

Do you think this is normal behavior? If so, what should I change so that it takes into account the current value !g1?

PS: Perhaps one solution is to create a function let f v = vin zone.ml, and then a maincall Zone.f !g1. But I have several global reference variables as g1in global.ml, I hope that they can be valid for all files and functions, and I do not want them to participate in the function signature.

+5
source share
1 answer

You are mostly concerned about how to evaluate top-level values ​​in your modules. The order in which this occurs is not related to the order in which the files are compiled, but rather the order in which they appear when linking files.

If you ignore the boundaries of the module, if you link the files in the order that you give, then you have the following:

let g1 = ref 0
let f = !g1
let () = g1 := 5

No wonder fit matters 0.

, main , . . , main ( ).

( , main - , C, . main. OCaml .)

Edit:

, , . , f zone.ml, , g1, .

- f zone.ml !g1.

f zone.ml, , . :

let f () = !g1

f zone.ml f.

+6

All Articles