From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q0H4ART9029357 for ; Tue, 17 Jan 2012 05:10:27 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ar0BADL0FE/RVdY2kGdsb2JhbABEnQiPKIEECCIBAQEBCQkNBxQEIYFyAQEBBBICExkBGxILAQMMBgULDQ0hIgERAQUBChIGExIQh2CZGAqLaoJvhGE/iHECBQuDcYRzAQQCAgUIAgMBBAcIAgEBAwUIAwQNBQsEAgECAQENBQQRBQEGAQEGAQUUBQcBCwECAQEIAQEBAQKDBRoCBwEBAgMNAQIDAQEDAgMCAwQBBINHBIg7jFaOCj2EGw X-IronPort-AV: E=Sophos;i="4.71,521,1320620400"; d="scan'208";a="139986199" Received: from mail-bk0-f54.google.com ([209.85.214.54]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Jan 2012 05:10:00 +0100 Received: by bke17 with SMTP id 17so1950912bke.27 for ; Mon, 16 Jan 2012 20:10:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=jVMa8Re7C1T4YUEU7YcRZNm30SwFaoeEwwwD0AEjKnE=; b=qHo6d5UeEn7o39dpkj3FaFM6fDIOpGT164qFgu+Y8Q6+C0cMZno5eQBagamY1+utsM DZLby50j1Z2xQS1yEg06P1eqcM14aIOzkkX6e7yzPVXrE2XdnZaXH2DJlk7MI+TOf19X z/X8mbIwsgCb5ny5oNdJFyOEem42amdKsIA3E= MIME-Version: 1.0 Received: by 10.205.118.4 with SMTP id fo4mr1311016bkc.139.1326773399996; Mon, 16 Jan 2012 20:09:59 -0800 (PST) Received: by 10.204.171.79 with HTTP; Mon, 16 Jan 2012 20:09:59 -0800 (PST) In-Reply-To: <4F14DBDC.5080600@crans.org> References: <4F14DBDC.5080600@crans.org> Date: Mon, 16 Jan 2012 20:09:59 -0800 Message-ID: From: Martin DeMello To: Pat Johnson Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by walapai.inria.fr id q0H4ART9029357 Subject: Re: [Caml-list] writing a very basic plugin system 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 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 >