[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | ronniec95@l... |
| Subject: | Re: [Caml-list] Module/functor question |
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