From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q0H2SS4h018137 for ; Tue, 17 Jan 2012 03:28:28 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkgDAKjbFE+K54gDgWdsb2JhbABEqwuBJYEMIgEBFiYlgXIBAQUnCwEFQBELIRQCDwkDAgECAUUTCAEBvgGJKwEBBQMEDQULBAIEAQUIBQQRBQEGAQEGAQUHJQECAQEFAwEBAQECFhUDAQYMBwICAx0DAQYJAgENAQEDCwILAgsDAQEJgTkGA3yDHASVEYVRjHE X-IronPort-AV: E=Sophos;i="4.71,521,1320620400"; d="scan'208";a="127500252" Received: from rouge.crans.org ([138.231.136.3]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 17 Jan 2012 03:28:23 +0100 Received: from localhost (localhost.crans.org [127.0.0.1]) by rouge.crans.org (Postfix) with ESMTP id 8166D8285 for ; Tue, 17 Jan 2012 03:28:22 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at crans.org Received: from rouge.crans.org ([10.231.136.3]) by localhost (rouge.crans.org [10.231.136.3]) (amavisd-new, port 10024) with LMTP id 3Op7+HkzJ94c for ; Tue, 17 Jan 2012 03:28:22 +0100 (CET) Received: from [138.231.143.117] (pistache.crans.org [138.231.143.117]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by rouge.crans.org (Postfix) with ESMTPSA id 53D4C80E4 for ; Tue, 17 Jan 2012 03:28:22 +0100 (CET) Message-ID: <4F14DBDC.5080600@crans.org> Date: Tue, 17 Jan 2012 03:24:28 +0100 From: Pat Johnson Organization: BdA User-Agent: Mozilla/5.0 (X11; Linux i686; rv:9.0) Gecko/20111224 Thunderbird/9.0.1 MIME-Version: 1.0 To: caml-list@inria.fr References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [Caml-list] writing a very basic plugin system 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