* [Caml-list] writing a very basic plugin system
@ 2012-01-17 0:04 Martin DeMello
2012-01-17 2:24 ` Pat Johnson
0 siblings, 1 reply; 3+ messages in thread
From: Martin DeMello @ 2012-01-17 0:04 UTC (permalink / raw)
To: OCaml List
What is the best mechanism to write the following basic plugin system?
Plugins should provide two functions, `usage ()` and `handle :
[string] -> unit`, and the main interpreter will receive args of the
form `plugin-name [arg1; arg2; ...]. The problem with the main handle
function below is that it's twice as long as necessary - I should be
able to capture the pattern "if there are 0 args call usage else call
handle" more succinctly. Ideally, I just want to have a mapping of
plugin name to plugin and have the rest of the code handled
generically.
module A = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from A"
end
module B = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from B"
end
let handle m args = match (m, args) with
"a", [] -> A.usage ()
| "a", xs -> A.handle xs
| "b", [] -> B.usage ()
| "b", xs -> B.handle xs
| _, _ -> print_endline "no such module"
let _ =
handle "a" [];
handle "a" ["1"];
handle "b" [];
handle "b" ["args"]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] writing a very basic plugin system
2012-01-17 0:04 [Caml-list] writing a very basic plugin system Martin DeMello
@ 2012-01-17 2:24 ` Pat Johnson
2012-01-17 4:09 ` Martin DeMello
0 siblings, 1 reply; 3+ messages in thread
From: Pat Johnson @ 2012-01-17 2:24 UTC (permalink / raw)
To: caml-list
Le 17/01/2012 01:04, Martin DeMello a écrit :
> What is the best mechanism to write the following basic plugin system?
> Plugins should provide two functions, `usage ()` and `handle :
> [string] -> unit`, and the main interpreter will receive args of the
> form `plugin-name [arg1; arg2; ...]. The problem with the main handle
> function below is that it's twice as long as necessary - I should be
> able to capture the pattern "if there are 0 args call usage else call
> handle" more succinctly. Ideally, I just want to have a mapping of
> plugin name to plugin and have the rest of the code handled
> generically.
This can easily be dealt with 3.12 capabilities :
module type Plugin =
sig
val usage : unit -> unit
val handle : string list -> unit
end;;
(* let's assume A and B are declared accordingly *)
let handle m args =
let module Plug = val
begin
match m with
"a" -> A
"b" -> B
| _-> raise Not_found
end : Plugin
in match args with
[] -> Plug.usage ()
| xs -> Plug.handle xs;;
Hope this helps,
--
PJ
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] writing a very basic plugin system
2012-01-17 2:24 ` Pat Johnson
@ 2012-01-17 4:09 ` Martin DeMello
0 siblings, 0 replies; 3+ messages in thread
From: Martin DeMello @ 2012-01-17 4:09 UTC (permalink / raw)
To: Pat Johnson; +Cc: caml-list
Perfect, thanks! Here's the complete working example (with help from
http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc81):
module type Plugin =
sig
val usage : unit -> unit
val handle : string list -> unit
end;;
module A = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from A"
end
module B = struct
let usage () = print_endline "usage of A"
let handle args = print_endline "hello from B"
end
let handle m ?(args = []) () =
let module Plug = (val
begin
match m with
"a" -> (module A: Plugin)
| "b" -> (module B: Plugin)
| _-> raise Not_found
end : Plugin)
in match args with
[] -> Plug.usage ()
| xs -> Plug.handle xs;;
let _ =
handle "a" ();
handle "a" ~args:["foo"] ();
handle "b" ();
handle "b" ~args:["foo"] ();
handle "c" (); (* => error *)
martin
On Mon, Jan 16, 2012 at 6:24 PM, Pat Johnson <johnson@crans.org> wrote:
> Le 17/01/2012 01:04, Martin DeMello a écrit :
>
>> What is the best mechanism to write the following basic plugin system?
>> Plugins should provide two functions, `usage ()` and `handle :
>> [string] -> unit`, and the main interpreter will receive args of the
>> form `plugin-name [arg1; arg2; ...]. The problem with the main handle
>> function below is that it's twice as long as necessary - I should be
>> able to capture the pattern "if there are 0 args call usage else call
>> handle" more succinctly. Ideally, I just want to have a mapping of
>> plugin name to plugin and have the rest of the code handled
>> generically.
>
>
> This can easily be dealt with 3.12 capabilities :
>
> module type Plugin =
> sig
> val usage : unit -> unit
> val handle : string list -> unit
> end;;
>
> (* let's assume A and B are declared accordingly *)
>
>
> let handle m args =
> let module Plug = val
> begin
> match m with
> "a" -> A
> "b" -> B
> | _-> raise Not_found
> end : Plugin
> in match args with
> [] -> Plug.usage ()
> | xs -> Plug.handle xs;;
>
> Hope this helps,
>
> --
> PJ
>
>
> --
> Caml-list mailing list. Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-01-17 4:10 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-17 0:04 [Caml-list] writing a very basic plugin system Martin DeMello
2012-01-17 2:24 ` Pat Johnson
2012-01-17 4:09 ` Martin DeMello
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox