[ocaml-platform] on the need and design of OCaml namespaces

Christophe TROESTLER Christophe.Troestler at umons.ac.be
Mon Feb 25 18:21:20 GMT 2013


On Mon, 25 Feb 2013 14:15:54 +0000, Anil Madhavapeddy wrote:
> 
> On 24 Feb 2013, at 19:26, Christophe TROESTLER <Christophe.Troestler at umons.ac.be> wrote:
> 
> > Camlp4 can insert some code to alias the standard modules needed
> > by code generation at the beginning of the source files (not
> > foolproof because a name needs to be generated but good enough in
> > practice).  It would be better if that facility was provided by a
> > Camlp4 module instead of needing to be redone by each extension.
> 
> That's an interesting idea.  The only hitch is that it's a little
> hard to do in one pass, as the code generation is called on the
> local AST fragment.

It can be done in one pass thanks to AstFilters.register_str_item_filter.  
Example code:

    let declarations_at_beginning = ref []
    
    (* The filters are evaluated after the whole source is read, thus all
       constants will have been collected. *)
    let () =
      if not_interactive then begin
        let add_top_declarations str_item =
          let add s decl = <:str_item at here< $decl$ $s$ >> in
          List.fold_left add str_item !declarations_at_beginning in
        AstFilters.register_str_item_filter add_top_declarations
      end
    
    let add_to_beginning_of_file decl =
      declarations_at_beginning := decl :: !declarations_at_beginning


More information about the Platform mailing list