From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA08765; Sun, 14 Mar 2004 19:52:53 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 TAA08896 for ; Sun, 14 Mar 2004 19:52:52 +0100 (MET) From: ronniec95@lineone.net Received: from localhost.home.net (chowdhury95.demon.co.uk [80.177.9.225]) by nez-perce.inria.fr (8.12.10/8.12.10) with SMTP id i2EIrFKW003799 for ; Sun, 14 Mar 2004 19:53:15 +0100 Received: (qmail 1913 invoked by uid 1000); 14 Mar 2004 19:05:28 -0000 Date: Sun, 14 Mar 2004 19:05:28 +0000 To: caml-list@inria.fr Subject: [Caml-list] Module/functor question Message-ID: <20040314190527.GA1898@cradle> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4i X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ronniec:01 lineone:01 functor:01 acheive:01 functors:01 foo:01 foo:01 typename:01 printf:01 acheive:01 invokes:01 mli:01 val:01 val:01 mli:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 131 Hello I'm trying to acheive the following effect in Ocaml that I should be able to do but am getting very confused with modules and functors... I have a set of classes all with the structure:- class Foo { Foo* create(std::string x); std::string toXml(Foo*); ... /* Other methods */ int m_x; std::string m_y; } and I have a template function template void serialisetoxml(T* x) { printf("%s\n",x->toXml().c_str());} int main() { Foo* f = new Foo(1,"hello"); serialisetoxml(f); } The effect I want to acheive is to have a function that takes any module that has a toXml function defined and invokes it somewhere. The closest I got was (*serialise.mli*) type t (* this doesn't work...*) module type Serialise sig val create : Xml.xml -> t (*Using XML-Light here *) val to_xml : t -> Xml.xml end (*foo.mli*) type t = { x : int ; y:int } module Make sig val create : Xml.xml -> t (*Using XML-Light here *) val to_xml : t -> Xml.xml end (*foo.ml*) type t = { x : int ; y:int } module Make : Serialiser = struct let create (f: Xml.xml) = Xml.Element("Foo",[("x",string_of_int f.x);("y",f.y))]) let to_xml (foo : t) = ... (* It thinks f is of type Serialiser.t and not Foo.t *) end (* main.ml *) module Sender (C : Serialiser) = struct let serialisetoxml msg = print_string (Xml.to_string (C.as_xml msg)) end (* was hoping for something like this *) let _ = let m = module Sender(Foo.Make) in m.serialisetoxml (Foo.Make (Xml.parse_from "...")); but I cannot figure out how to declare Foo having a signature of Serialiser and then use it in main somehow. Assume also that I have lots of other modules which have same basic interface but additional methods specific to the modules. Any help appreciated. Thanks, Ronnie -- ------------------- 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