English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] load modules by name
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2003-01-23 (10:27)
From: michael-grunewald@w...
Subject: [Caml-list] Re: load modules by name
Pietro Abate <Pietro.Abate@anu.edu.au> writes:

> hi list,
> I'm trying to figure out how I can build a kind of associative list
> to load a specific module at runtime. I've reread few old messages about
> first-class modules and other oddities, but I've the sensation my
> problem is easier.
> I have a library (compiled), an application (compiled) and a bunch of
> user defined modules (that are compiled as well, but these may be vary
> in number). At the moment the application select the rigth module
> statically regarding a command line argument. However this way I must
> recompile my application everytime I add a new module.

Why don't you build a little bunch of software that maintain a 'record
book' of given modules/files? Thus the client software find easily a
list in the 'record book', while you just need to update this book each
time you add a new algorithm into your collection (or remove one).

In the main code part, you may have a module

module Algo =

    type t = int -> int list (* e.g. *)
    type id = int

    let assoc = ref [] (* (id * algo) list -- assoc *)
    let subscribe a b = assoc := (a,b) :: !assoc
    let get i = List.assoc i !assoc


each module dynamically loaded has a registration part that says

let _ = Algo.subscription my_id my_algo

where my_id is an unique identification key known in the record book,
thus you can get back the algorithm with the Algo.get function, while
you keep away from your code any reference to hardwired module name.
Michaël Grünewald <michael-grunewald@wanadoo.fr>
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