Loading ...
Sorry, an error occurred while loading the content.

1403Re: "ocaml_beginners"::[] Self-referencing array init

Expand Messages
  • Remi Vanicat
    Oct 1, 2003
      Alan Post <apost@...> writes:

      > ocaml 3.06
      > I'm sure there's a simple way to do this, but I just can't think of
      > it.
      > I want to do something like:
      > let arr = Array.init 10 (...use arr...)
      > In my case, I want to stick a bunch of objects in an array, and
      > reference that array in one of their callbacks. A simplified (and
      > less sensible) version would look something like:
      > let arr = Array.init 10
      > (fun i -> Some (fun () -> if arr.(9-i) = None then
      > prerr_endline "Ouch" ))

      you could look to :


      and use one of the functions Pierre Weis defines there.
      or make you own.

      > When I feed this to ocaml, it complains about arr being unbound.
      > Doing a "let rec" yields the following amusing message:
      > This kind of expression is not allowed as right-hand side of `let
      > rec'

      Ocaml make a lot of statical check. So he forbid what you are trying
      (the right-hand side of let rec must not be the result of the
      evaluation of some function).

      > In the silly case above, there's a reasonable default value, but
      > that's not the case in my actual (lablgtk2) program.

      I don't know what you are trying exactly, but why don't you create
      first your array, then iterate over it for creating your callback.

      RĂ©mi Vanicat
    • Show all 11 messages in this topic