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
Is this use of Object.magic safe?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-11-15 (21:38)
From: Anton Moscal <msk@p...>
Subject: Re: Is this use of Object.magic safe?
On Mon, 13 Nov 2000, Stephan Houben wrote:

> I have three questions:
> 1. Is this safe w.r.t. the garbage collector? Or am I going to confuse the GC?
> 2. Can I safely marshall values?
> 3. Is it worth the trouble? Perhaps O'Caml already does something smart
>    when a constructor of the form `Int of int' is encountered?

Answers for 1 & 2 is true,
for 3 - false: if you try to run the following program:

let _ = Printf.printf "%b\n" 
  (value_of_int 0 == value_of_allocated_value True)

you got "true" as the result.

The cause is the following: variant without arguments 
represented in the same way with int's.

Correct solution will be:

type allocated_value =
  Bool of bool 
| String of string 
| Array of value

Anton Moscal