Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] static variables in a function
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@c...>
Subject: Re: [Caml-list] static variables in a function
>>>>> "mk" == Max Kirillov <max630@mail.ru> writes:

    mk> Hello.  The code you write will generate a new (empty) ref at
    mk> every call.

Actually, it won't.  Take a look at the code that was sent by Yutaka OIWA:

let get_chunk =
  let chunks_list = ref [] in
  fun () ->
    ...

The ref is bound outside of the function definition, so it's created
once and kept in the closure, not allocated in each call.  If it were
written this way:

let get_chunk () =
  let chunks_list = ref [] in
  ...

or equivalently:

let get_chunk =
  fun () ->
    let chunks_list = ref [] in
    ...

it would be created separately for each call.  But the way it was
originally written is correct.

Your version also works, but creates a module-level binding for the
value as well, which isn't desirable for something like this, where
you want the value to be local to the function definition.

John.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners