caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] marshalling and code changes
@ 2002-07-29 12:50 Oleg
  2002-07-29 13:48 ` James Leifer
  0 siblings, 1 reply; 2+ messages in thread
From: Oleg @ 2002-07-29 12:50 UTC (permalink / raw)
  To: caml-list

Hi

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?

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

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.


Thanks
Oleg
-------------------
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] marshalling and code changes
  2002-07-29 12:50 [Caml-list] marshalling and code changes Oleg
@ 2002-07-29 13:48 ` James Leifer
  0 siblings, 0 replies; 2+ messages in thread
From: James Leifer @ 2002-07-29 13:48 UTC (permalink / raw)
  To: Oleg, caml-list

Greetings Oleg,

The problem you raise is one that I (and others here: Gilles.Peskine@inria.fr,
Jun.Furuse@inria.fr, 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
types...

Warmest wishes,

James Leifer
INRIA Rocquencourt



-------------------
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-29 13:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-29 12:50 [Caml-list] marshalling and code changes Oleg
2002-07-29 13:48 ` James Leifer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).