caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Stephane Glondu <Stephane.Glondu@crans.org>
To: jtbryant@valdosta.edu
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Marshal and Polymorphism
Date: Thu, 04 Aug 2005 14:57:44 -0700	[thread overview]
Message-ID: <42F28F58.70609@crans.org> (raw)
In-Reply-To: <1123187967.3276.94.camel@starlight.valdosta.edu>

Jonathan Bryant wrote:
> Yeah, I'm sorry I wasn't real clear.  Let me try again:
> 
> This code works:
> 
> module Test : [...]
> 
> let _ =
>     let x = Test.create "Hello" 1 in
>     let ser_data = Test.serialize x in
>     let deser_data = Test.deserialize ser_data in
>     Printf.printf "%s: %d\n" (Test.get_word deser_data) (Test.get_index
> deser_data);
> ;;
> 
> My question is:  Will this /always/ work (given that the type of data I
> read out of the file is the same)?

It depends on what you mean by "work". Types are not marshaled.

Hence, your deserialize will return ('a, 'b) t (or something like
('_a, '_b) t) in the same way Marshal.from_string returns 'a, which is
meaningless. So you should *always* explicitly give the actual type
(without type variable) when you call deserialize, i.e. your test code
should be:

let _ =
    let x = Test.create "Hello" 1 in
    let ser_data = Test.serialize x in
    let deser_data = (Test.deserialize ser_data :
                        (string, int) Test.t) in
    ... ;;

Otherwise, you could write something like:

let _ =
    let x = Test.create "Hello" 1 in
    let ser_data = Test.serialize x in
    let deser_data = Test.deserialize ser_data in
    (Test.get_index deser_data) ();;

and get a runtime crash (you wouldn't be able to call "deser_data ()"
directly, though).

> Is it really that simple?

As simple as using Marshal.* functions: if you are aware of the traps...


BTW, here, giving the return type in
(Marshal.from_string x 0 : ('a, 'b) t)
is useless, since it is enforced by the signature.


I hope this will help,

-- 
Stephane Glondu


  reply	other threads:[~2005-08-04 21:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-04 18:23 Jonathan Bryant
2005-08-04 20:06 ` [Caml-list] " Stephane Glondu
2005-08-04 20:39   ` Jonathan Bryant
2005-08-04 21:57     ` Stephane Glondu [this message]
2005-08-04 23:17     ` skaller

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=42F28F58.70609@crans.org \
    --to=stephane.glondu@crans.org \
    --cc=caml-list@inria.fr \
    --cc=jtbryant@valdosta.edu \
    /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).