Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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 <> 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.


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: