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: Max Kirillov <max630@m...>
Subject: Re: [Caml-list] static variables in a function
Hello.

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

You can do like this:

--- chunks.ml
type chunk = ...
<...>
let chunks_list = ref ([]:chunk ref);; -- a separate value in a module
let get_chunk () = <youraction>
--- EOF

You can hide chunks_list by module interface.

--- chunks.mli
type chunk
<...>
(*val chunks_list: chunk list ref -- comment it away *)
val get_chunk: unit -> <returntype>
--- EOF

On Sat, Jun 15, 2002 at 02:58:45AM +0900, Yutaka OIWA wrote:
> let get_chunk =
>   let chunks_list = ref [] in
>   fun () ->
>     ...
> 

On Fri, Jun 14, 2002 at 10:08:30AM -0700, Shannon --jj Behrens wrote:
> What is the correct OCAML idiom for achieving the
> following psuedocode?
> 
> let get_chunk () =
>   static chunks_list;
> 
>   if List.is_empty chunks_list
>   then chunks_list = get_more_chunks ();
>   shift chunks_list
> ;;
-------------------
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