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
Extending modules and signatures
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-04-17 (21:36)
From: Goswin von Brederlow <goswin-v-b@w...>
Subject: Re: [Caml-list] Extending modules and signatures
Peter Hawkins <hawkinsp@cs.stanford.edu> writes:

> Hi...
> I have a quick question. I want to extend the List module with various
> functions that I want that aren't present in the standard library,
> much as the Batteries ExtList library does.
> I might write the following code in "mylibrary.ml":
> module MyList = struct
>   include List
>   let foo x = ... code here
>   let bar y = ... code here
> end
> That's ok so far, but now suppose I want to write a "mylibrary.mli"
> interface file corresponding to "mylibrary.ml". Ideally I'd write
> something like this in "mylibrary.mli":
> module MyList : sig
>   include List  (* unknown module type List *)
>   val foo : ...
>   val bar : ...
> end
> Unfortunately I can't include "List" here since it is a structure, not
> a signature. I don't think there is a way to say "include the
> signature associated with List".
> I can think of three solutions:
> a) Copy the complete signature of List into MyList. This is a bad idea
> since the List module might change in the future. This is what the
> Batteries ExtList module does.
> b) Alter the List module to define a signature, say List.S, in
> addition to its other contents. I can't easily do this since I didn't
> write the List module.
> c) Don't write a .mli file at all.
> Are there any other alternatives?

Something like :

module MyList = struct
  include List
  module M : sig val foo : int -> unit end = struct
    let foo x = ()
    let bar y = y
  include M

and no .mli file. The module M is there to alow you to give (or hide)
the signature of just your extra functions.

> Cheers,
> Peter