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

1402Self-referencing array init

Expand Messages
  • Alan Post
    Oct 1 3:07 AM
    • 0 Attachment
      ocaml 3.06

      I'm sure there's a simple way to do this, but I just can't think of

      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" ))

      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'

      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 like the following alternative:

      let arr = ref None
      in arr := Some (Array.init 10 ...)
    • Show all 11 messages in this topic