caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: James Leifer <James.Leifer@inria.fr>
To: Oleg <oleg_inconnu@myrealbox.com>, caml-list@inria.fr
Subject: Re: [Caml-list] marshalling and code changes
Date: Mon, 29 Jul 2002 15:48:33 +0200	[thread overview]
Message-ID: <r77znwa7iha.fsf@brouilly.inria.fr> (raw)
In-Reply-To: <200207291249.IAA02832@dewberry.cc.columbia.edu> (Oleg's message of "Mon, 29 Jul 2002 08:50:26 -0400")

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


      reply	other threads:[~2002-07-29 13:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-29 12:50 Oleg
2002-07-29 13:48 ` James Leifer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=r77znwa7iha.fsf@brouilly.inria.fr \
    --to=james.leifer@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=oleg_inconnu@myrealbox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).