From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 5C0B5BBD7 for ; Thu, 4 Aug 2005 23:59:38 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j74Lxb4K009032 for ; Thu, 4 Aug 2005 23:59:37 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA27760 for ; Thu, 4 Aug 2005 23:59:37 +0200 (MET DST) Received: from mx5.ucdavis.edu (mx5.ucdavis.edu [169.237.104.15]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j74LxZ14009018 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Thu, 4 Aug 2005 23:59:36 +0200 Received: from [169.237.7.95] (puck.cs.ucdavis.edu [169.237.7.95]) by mx5.ucdavis.edu (8.13.3/8.13.1/it-defang-5.4.0) with ESMTP id j74LxXHf015377; Thu, 4 Aug 2005 14:59:33 -0700 (PDT) Message-ID: <42F28F58.70609@crans.org> Date: Thu, 04 Aug 2005 14:57:44 -0700 From: Stephane Glondu User-Agent: Debian Thunderbird 1.0.2 (X11/20050331) X-Accept-Language: en-us, en MIME-Version: 1.0 To: jtbryant@valdosta.edu Cc: caml-list@inria.fr Subject: Re: [Caml-list] Marshal and Polymorphism References: <1123179836.3276.76.camel@starlight.valdosta.edu> <42F2752C.9070904@crans.org> <1123187967.3276.94.camel@starlight.valdosta.edu> In-Reply-To: <1123187967.3276.94.camel@starlight.valdosta.edu> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.49 on 169.237.104.15 X-Miltered: at nez-perce with ID 42F28FC9.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 42F28FC7.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 polymorphism:01 serialize:01 deserialize:01 printf:01 printf:01 marshaled:01 deserialize:01 serialize:01 runtime:01 ...:98 ...:98 wrote:01 marshal:01 marshal:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 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