* The sharing mechanism in the Marshal module
@ 2007-03-03 14:59 Deokhwan Kim
2007-03-04 14:38 ` Zheng Li
2007-03-07 14:59 ` [Caml-list] " Roland Zumkeller
0 siblings, 2 replies; 3+ messages in thread
From: Deokhwan Kim @ 2007-03-03 14:59 UTC (permalink / raw)
To: caml-list
Hello,
I thought the following code would produce 2 in (* 1 *), but as it is it
outputs 1:
let incr =
let c = ref 0 in
function () -> c:= !c + 1 ; !c
let save =
Marshal.to_string incr [Marshal.Closures]
let incr1 =
(Marshal.from_string save 0 : unit -> int)
let () =
Printf.printf "%d\n" (incr ()) ;
Printf.printf "%d\n" (incr1 ()) (* 1 *)
Why? In addtion, what should I do if I really want the sharing of the c
counter between incr and incr1?
Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: The sharing mechanism in the Marshal module
2007-03-03 14:59 The sharing mechanism in the Marshal module Deokhwan Kim
@ 2007-03-04 14:38 ` Zheng Li
2007-03-07 14:59 ` [Caml-list] " Roland Zumkeller
1 sibling, 0 replies; 3+ messages in thread
From: Zheng Li @ 2007-03-04 14:38 UTC (permalink / raw)
To: caml-list
Hi,
Deokhwan Kim <dk@ropas.snu.ac.kr> writes:
> I thought the following code would produce 2 in (* 1 *), but as it is it
> outputs 1:
That's the semantic of marshal, it serialize functional values as *closures*,
i.e. functions carrying its whole environments. In your example, the incr1
also gets a copy of incr's environment, which has a copy of c included.
Despite some flaws being spotted in the marshal module last month [1], as far
as your example goes, you should stick with the closure semantics.
> Why? In addtion, what should I do if I really want the sharing of the c
> counter between incr and incr1?
There are some workarounds, but it depends on why you need marshaling. In
many cases, you don't actually need marshal for a shallow copy.
[1] http://caml.inria.fr/pub/ml-archives/caml-list/2007/02/84333195e90a8f13ac9fea7fb6a706bf.en.html
--
Zheng Li
http://www.pps.jussieu.fr/~li
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] The sharing mechanism in the Marshal module
2007-03-03 14:59 The sharing mechanism in the Marshal module Deokhwan Kim
2007-03-04 14:38 ` Zheng Li
@ 2007-03-07 14:59 ` Roland Zumkeller
1 sibling, 0 replies; 3+ messages in thread
From: Roland Zumkeller @ 2007-03-07 14:59 UTC (permalink / raw)
To: caml-list
On 03/03/07, Deokhwan Kim <dk@ropas.snu.ac.kr> wrote:
> In addtion, what should I do if I really want the sharing of the c
> counter between incr and incr1?
It suffices to define "c" outside of "incr":
let c = ref 0;;
let incr () = c:= !c + 1 ; !c
--
http://www.lix.polytechnique.fr/~zumkeller/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-03-07 14:59 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-03 14:59 The sharing mechanism in the Marshal module Deokhwan Kim
2007-03-04 14:38 ` Zheng Li
2007-03-07 14:59 ` [Caml-list] " Roland Zumkeller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox