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] marshalling and code changes
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-07-29 (13:59)
From: James Leifer <James.Leifer@i...>
Subject: Re: [Caml-list] marshalling and code changes
Greetings Oleg,

The problem you raise is one that I (and others here:,, and Pierre.Weis@inria) have been thinking about.

> Suppose I use Marshall module (and input_value / output_value pervasives) to 
> serialize a sophisticated data type. If I make [small] changes to this data 
> type, what is the best way to convert old saved data?

Currently if you want to be safe, you need to read the data back under
precisely the same type used to write it.  You can then do the conversion by
writing the appropriate Ocaml function.

> What if the new type is a supertype to the old one, can I load it safely?

Nothing is guaranteed, currently.

> I tried it with 
> type old_type = Float of float | Int of int ;;
> type new_type = Float of float | Int of int | S of string;;
> and it seemed to have worked with input_value and output_value.

Input_value and output_value are not safe (as stated in the manual): the fact
that this worked is a ``fluke'' due to the way values of unions are
represented.  If you go in the other direction (saving S "hello" and reloading
it as old_type), you'll get a crash, as illustrated by the following example:
Open two terminal windows each running an ocaml top-level. In the first type:

   type new_type = Float of float | Int of int | S of string;;
   let fd = open_out "/tmp/testfile" in
   output_value fd (S "hello"); close_out fd;;

Then in the second type:

   type old_type = Float of float | Int of int;;
   let fd = open_in "/tmp/testfile" in 
   match input_value fd with Float _ -> "float" | Int _ -> "int";;

As a practical programming discipline, put all types you want to use for
input/output in a single module and coerce every read and write explicitly to
the types defined in that module.  If you do this systematically, you will be
able to detect any type errors at compile-time.

Another route is to use the many XML writers and parsers that have been
circulating the list recently.  These are unlikely to give you the same
performance though.

I'm working (with the above-mentioned colleagues) on run-time checking for
marshalling.  This gets hairy though if one wants to deal with abstract

Warmest wishes,

James Leifer
INRIA Rocquencourt

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