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 q0H04i9S015369 for ; Tue, 17 Jan 2012 01:04:44 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArsBAM66FE/RVdY0kGdsb2JhbABErDCBBAgiAQEBAQkJDQcUBCGCCwITGQEbHgMSEF0BEQEFARYMGxqeJoJeCotqgm+ETT+IcQIFC4knAwQNBQsEAgECAQENBQQRBQEGAQEGAQUUBQcBCwECAQEFAwEBAQECEhcxEUcEgU+EHASIO4xWjgo9hBs X-IronPort-AV: E=Sophos;i="4.71,520,1320620400"; d="scan'208";a="127442253" Received: from mail-bk0-f52.google.com ([209.85.214.52]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 17 Jan 2012 01:04:27 +0100 Received: by bkbzt4 with SMTP id zt4so1495377bkb.39 for ; Mon, 16 Jan 2012 16:04:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; bh=v3/KVjCT5CW5sS1tmMJGErcSIYUhTnBih+dv2rJhbQs=; b=QUm8SSdUxZT79ah00/Jr7/AWcSE6I59G4dYsD0tb5/7je5fVrwLPrV145ZmXJJ51xM goAwlOnYCntY4krlOT/OhQLlgw3itk2gQhYeoLJ+vjSqHTUXgMs+bjwAY6W0Z/EJF/El NZ6wMmzOrtKDqR8MdHVjD9LOlWm3AUaYaawH4= MIME-Version: 1.0 Received: by 10.204.154.86 with SMTP id n22mr5644172bkw.85.1326758666576; Mon, 16 Jan 2012 16:04:26 -0800 (PST) Received: by 10.204.171.79 with HTTP; Mon, 16 Jan 2012 16:04:26 -0800 (PST) Date: Mon, 16 Jan 2012 16:04:26 -0800 Message-ID: From: Martin DeMello To: OCaml List Content-Type: text/plain; charset=ISO-8859-1 Subject: [Caml-list] writing a very basic plugin system 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"]