Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7431953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:22:41+00:00 2026-05-29T09:22:41+00:00

My folder contains several files, which are compiled in this order: global.ml , zone.ml

  • 0

My folder contains several files, which are compiled in this order: global.ml, zone.ml, abs.ml, main.ml

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

In zone.ml there is a declaration let f = !g1.

In abs.ml, there is g1 := 5, which will be run by main in the beginning of run-time, I consider it as an initialization of g1 given the real run-time context.

Later main will call Zone.f. Curiously, what I realize is that it takes f = 0 instead of f = 5.

Do you think this behavior is normal? If so, what should I change, to make it take the current value of !g1 into account?

PS: Maybe one solution is to make a function let f v = v in zone.ml then let main call Zone.f !g1. But I have several global reference variables as g1 in global.ml, I hope they could be valid over all the files and functions, and I don’t want to get them involved in the signature of a function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:22:42+00:00Added an answer on May 29, 2026 at 9:22 am

    You are basically concerned with the order of evaluation of the top-level values in your modules. The order in which this happens isn’t related to the order that you compile the files, but rather the order that they appear when you link the files.

    If you ignore the module boundaries, if you link the files in the order you give, what you have is like this:

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

    It shouldn’t be surprising that f has the value 0.

    Note that your main is not necessarily the first thing that happens at runtime. Top-level values are evaluated in the order the files appear when you link them. Very commonly, main is the last top-level thing to happen (because its file is usually the last one).

    (Also note that having a main at all is just a convention, presumably adopted by former C programmers like me. There’s no requirement to have a function named main. OCaml just evaluates the top-level values in order.)

    Edit:

    It’s difficult to say how to restructure your code without knowing more about it. The essence of your problem appears to be that you define f as a top-level immutable value in zone.ml but you want its value to follow g1, which is a mutable value.

    The simplest suggestion would be to remove the definition of f from zone.ml and replace it everywhere in the file with !g1.

    If you want to retain the name f at the top level in zone.ml, you have to redefine it as something other than an immutable value. A function is the most obvious choice:

    let f () = !g1
    

    Then you would replace uses of f in zone.ml by f () instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c:\config folder which contains several configuration files (config_x). I would like
i have a folder in sd card which contains several files. now i need
So I have this php web app, and one of my folder contains some
I have a parent folder called myfiles which contains several children folders. Each of
I have a folder in my C: drive as C:\app_config\java_app This folder contains some
I have write a python library app(which contains several *.py files). And several of
I want to delete a folder that contains thousands of files and folders. If
In our environment we have a Lib folder which contains various third party assemblies
I have a folder FolderA which contains three sub-folders: foldera1 foldera2 and foldera3 I
I have a folder in NTFS that contains tens of thousands of files. I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.