caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: ronniec95@lineone.net
To: Yamagata Yoriyuki <yoriyuki@mbg.ocn.ne.jp>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Module/functor question
Date: Sun, 14 Mar 2004 23:46:30 +0000	[thread overview]
Message-ID: <20040314234630.GA2668@cradle> (raw)
In-Reply-To: <20040315.042218.07646200.yoriyuki@mbg.ocn.ne.jp>

On Mon, Mar 15, 2004 at 04:22:18AM +0900, Yamagata Yoriyuki wrote:
> From: ronniec95@lineone.net
> Subject: [Caml-list] Module/functor question
> Date: Sun, 14 Mar 2004 19:05:28 +0000
> 
> > 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.
> 
> You do not need to declare.  Foo.Make will accept any module whose
> signature is an *extension* of Serializer.
> 
> > (* was hoping for something like this *)
> > let _ = let m = module Sender(Foo.Make) in
> > 	m.serialisetoxml (Foo.Make (Xml.parse_from "..."));
> 
> By the way, a correct code would be
> 
>    let _ =
>       let module M = Sender(Foo.Make) in
>       M.serialisetoxml (Foo.Make (Xml.parse_from "..."));
> 
> --
> Yamagata Yoriyuki

Thanks that helped a lot - well it does work! but I'm a bit puzzled by
the extra declarations I had to do... 

I've copied the exact code below since it's not that long. I wonder
about header.mli which has 'type t=u' in it which was the only way of
getting it all to fit together. Why do I have to do that? Is there
another a more Ocaml way that I should be tackling this problem (I guess
a more design rather than implementation question...?)

Thanks for any ideas.

Ronnie


------- Code ----------
(*Header.mli*)
type u

module Make :
    sig
        type t = u
        val as_xml : t -> Xml.xml
        val create : Xml.xml -> t
    end

(* A function to construct Header.u objects *)
val make : ?version:int -> ?msgtypeid:int -> string -> string -> string -> u

(*Header.ml*)

(* some utility functions *)
let datetimefmt = "%Y%m%dT%H%M%S"
let find_node name root =
    let rec find (lst : Xml.xml list) res = match lst with
    | []    -> if List.length res = 0 
               then failwith ("Node not found:" ^ name)
               else res
    | Xml.Element(id,_,_) as hd::tl when id = name  -> find tl (hd::res)
    | _::tl                                         -> find tl res
    in
        find (Xml.children root) []



type u= { version : int; created : Calendar.t; environment : string; msgtypeid : int; msgtype : string; originatorid : string; machineid: string; }

(* OK I want everything to have this signature at least *)
module type Constructor =
    sig
        type t = u
        val as_xml : t -> Xml.xml
        val create : Xml.xml -> t
    end

module Make : Constructor =
struct
    type t = u (* why do I need this?*)
    let as_xml d = 
        Xml.Element("Header",[ 
                ("MsgVersion",(string_of_int d.version));
                ("Created",(fun x -> Printer.CalendarPrinter.sprint
                datetimefmt x)d.created);
                ("Environment",d.environment);
                ("MsgTypeId",(string_of_int d.msgtypeid));
                ("MsgType",d.msgtype)],
                [Xml.Element("Originator",[
                    ("OriginatorId",d.originatorid);
                    ("MachineId",d.machineid)],[])])

    let create (root:Xml.xml) : t =
        let originator = List.hd (find_node "Originator" root) in
        {   version = int_of_string(Xml.attrib root "MsgVersion");
            created = ((fun x -> Printer.CalendarPrinter.from_fstring datetimefmt x)
            (Xml.attrib root "Created"));
            environment = Xml.attrib root "Environment";
            msgtypeid = int_of_string(Xml.attrib root "MsgTypeId");
            msgtype = Xml.attrib root "MsgType";
            originatorid = Xml.attrib originator "OriginatorId";
            machineid = Xml.attrib originator "MachineId"; }

end

let make ?(version = 1) ?(msgtypeid=1) msgtype originator env =
        { version = version; created = Calendar.now(); environment = env; msgtypeid = msgtypeid; msgtype = msgtype; originatorid = originator; machineid = Unix.gethostname(); }

(*****************************************
 * main.ml - thanks to Yamagata Yoriyuki 
 *****************************************)

(* Don't like declaring this AGAIN! in main*)
module type Constructor =
    sig
        type t
        val as_xml : t -> Xml.xml
        val create : Xml.xml -> t
    end


module MessageSender (C : Constructor) =
    struct
        let send msg = print_string (Xml.to_string (C.as_xml msg))
    end

let _ = 
    let module M = MessageSender(Header.Make) in
    M.send (Header.make "Header" "TestMessage" "DEV") (* this works now!!*)

-------------------
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:[~2004-03-14 23:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-14 19:05 ronniec95
2004-03-14 19:22 ` Yamagata Yoriyuki
2004-03-14 23:46   ` ronniec95 [this message]
2004-03-15 12:34     ` Andreas Rossberg
2004-03-15 16:20       ` ronniec95

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=20040314234630.GA2668@cradle \
    --to=ronniec95@lineone.net \
    --cc=caml-list@inria.fr \
    --cc=yoriyuki@mbg.ocn.ne.jp \
    /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).