Version française
Home     About     Download     Resources     Contact us    
Browse thread
lazy application to Lazy.t
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Karl Zilles <zilles@1...>
Subject: Re: [Caml-list] lazy application to Lazy.t
Hal Daume III wrote:
> suppose I have
> 
>   val x : int ref Lazy.t
> 
> and I want to 'incr' it, but want to keep it lazy.  i.e., if i start with:
> 
>   let x = Lazy.lazy_from_fun (fun () -> ref 0)
> 
> then i run my lazy_incr on it, I want:
> 
>   let y = Lazy.force x
> 
> to return 1
> 
> but i only want 'incr' to be run when I Lazy.force x.  is there a way to 
> accomplish this?

Not the way you have it.  Because x is itself not a reference, you can't 
change its value by calling a function on it.

If instead you had val x : int Lazy.t ref, then it would be possible:

open Lazy;;

let x = ref (lazy (0));;
val x : int lazy_t ref = {contents = <lazy>}

let lazy_incr r = let current = !r in r:=lazy ((force current) + 1);;
val lazy_incr : int Lazy.t ref -> unit = <fun>

lazy_incr x;;
- : unit = ()

force !x;;
- : int = 1