From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 8AA55BBD7 for ; Thu, 4 Aug 2005 22:37:50 +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 j74KboXh002247 for ; Thu, 4 Aug 2005 22:37:50 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA26562 for ; Thu, 4 Aug 2005 22:37:49 +0200 (MET DST) Received: from mailx.valdosta.edu (mailx.valdosta.edu [168.18.130.251]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j74KbmVq010419 for ; Thu, 4 Aug 2005 22:37:49 +0200 Received: from starlight.valdosta.edu (starlight.valdosta.edu [168.18.148.146]) by mailx.valdosta.edu (8.13.4/8.13.4) with ESMTP id j74KblQR015386 for ; Thu, 4 Aug 2005 16:37:47 -0400 (EDT) (envelope-from jtbryant@valdosta.edu) Subject: Re: [Caml-list] Marshal and Polymorphism From: Jonathan Bryant Reply-To: jtbryant@valdosta.edu To: caml-list@inria.fr In-Reply-To: <42F2752C.9070904@crans.org> References: <1123179836.3276.76.camel@starlight.valdosta.edu> <42F2752C.9070904@crans.org> Content-Type: text/plain Organization: Valdosta State University Information Technology (System Operations) Date: Thu, 04 Aug 2005 16:39:27 -0400 Message-Id: <1123187967.3276.94.camel@starlight.valdosta.edu> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-16) Content-Transfer-Encoding: 7bit X-PMX-Version: 5.0.2.153301, Antispam-Engine: 2.0.3.2, Antispam-Data: 2005.8.4.24 X-Miltered: at nez-perce with ID 42F27C9E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42F27C9C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 polymorphism:01 sig:01 val:01 val:01 serialize:01 deserialize:01 struct:01 serialize:01 deserialize:01 printf:01 printf:01 forall:01 ...:98 ...:98 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 Yeah, I'm sorry I wasn't real clear. Let me try again: This code works: module Test : sig type ('a, 'b) t val create : 'a -> 'b -> ('a, 'b) t val serialize : ('a, 'b) t -> string val deserialize : string -> ('a, 'b) t val get_word : ('a, 'b) t -> 'a val get_index : ('a, 'b) t -> 'b end = struct type ('a, 'b) t = { word : 'a; index : 'b };; let create x y = { word = x; index = y };; let serialize x = Marshal.to_string x [];; let deserialize x = (Marshal.from_string x 0 : ('a, 'b) t);; let get_word x = x.word;; let get_index x = x.index;; end ;; 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)? Is it really that simple? On Thu, 2005-08-04 at 13:06 -0700, Stephane Glondu wrote: > Hi, > > Jonathan Bryant wrote: > > If I make the module have > > > > type = ('a, 'b) t > > > > in the module definition, can I use > > > > (Marshal.from_string ... : 'a) > > > > and get whatever 'a was defined as in the definition. > > In the way I understood your message, you cannot. However, if you do > something like : > > let get (table : ('a, 'b) t) index = > (Marshal.from_string ... : 'a) > > The 'a type variable must be bound /inside/ a surrounding function > (and (Marshal.from_string val : 'a) should be forbidden outside a > function). > > > For example, if > > it was a (string * int) t, would the function return a string? > > Now, yes. > > > Or would > > this be redundant (since Marshal.from_string already returns 'a)? > > No! Actually, the return type of Marshal.from_string is [forall 'a. > 'a], so it is different from any other 'a which could be already bound > at this moment. Therefore, you must explicitly tell the return type of > Marshall.from_string, even if it is polymorphic. > > I hope this is clear. > > Best regards, > -- *=========================* |Jonathan Bryant | |Valdosta State University| |Information Technology | |System Operations | |-------------------------| |jtbryant@valdosta.edu | |x6358 | *=========================*