caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: David Allsopp <dra-news@metastack.com>
To: "'Oliver Bandel'" <oliver@first.in-berlin.de>,
	"'caml-list@yquem.inria.fr'" <caml-list@yquem.inria.fr>
Subject: RE: [Caml-list] Code-execution (evaluation) time
Date: Tue, 19 Oct 2010 19:52:36 +0000	[thread overview]
Message-ID: <E51C5B015DBD1348A1D85763337FB6D92C56A773@Remus.metastack.local> (raw)
In-Reply-To: <20101019203918.202612lacw5rse6u@webmail.in-berlin.de>

Oliver Bandel wrote:
> Hello,
> 
> I want to refresh my OCaml knowledge after I (again) paused for a while.
> 
> I want to know, when certain parts of the code are executed,
> for example if I have more than one   let _ = ...
> statement in my code (for example in different comp0ilaion units each one
> of them).

Expressions (as in, things listed in http://caml.inria.fr/pub/docs/manual-ocaml/expr.html) are evaluated in the order in which they appear in a source file. If you have two files A.ml and B.ml which contain top-level statements, the order in which they execute will depend on the order in which you link the resulting .cmo or .cmx files (which should be left-to-right as passed to ocamlc/ocamlopt - don't know where that's specified).

As far as I can see, in pretty much all contexts within an expression where order of evaluation could matter including, but probably not limited to:

* Record initialisation (e.g. let i = ref 0 in {a = (incr i; !i); b = (incr i; !i)})
* Function application (similarly)
* let foo = _ and bar = _ in _

are intentionally left as unspecified (in practice, application and record initialisation are right-to-left and let x = _ and y = _ is left-to-right but the point is that you are never supposed to rely on that).

> I also found an old code snippet with the following parts:
> 
> ==============================
> let fnames = List.tl(Array.to_list Sys.argv)
> 
> (* some more code *)
> 
> let _ =
>    List.iter print_days_old fnames
> ==============================
> 
> So, the value fnames contains the args from the command line and can be
> used in the first statement of the code.

[let fnames = ..] is a complete expression so it's evaluated before the [let _] further down.


David


  reply	other threads:[~2010-10-19 19:54 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-19 18:39 Oliver Bandel
2010-10-19 19:52 ` David Allsopp [this message]
2010-10-21 19:43   ` [Caml-list] " oliver
2010-10-21 22:34   ` oliver
2010-10-19 20:50 mark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E51C5B015DBD1348A1D85763337FB6D92C56A773@Remus.metastack.local \
    --to=dra-news@metastack.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=oliver@first.in-berlin.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).