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
Lightweight way to extend existing module. Almost there?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2010-11-17 (04:10)
From: Till Varoquaux <till@p...>
Subject: Lightweight way to extend existing module. Almost there?
With the improvements to the module system in ocaml 3.12 extending a
signature just got much much lighter. Suppose I want the signature of
the module which in every respects is like StdLabels excepted that the
String module has an extra "is_prefix_of" I can write:

  module String : sig
    include module type of StdLabels.String
    val is_prefix_of : string ->  string ->  bool

  include module type of StdLabels with module String :=  String

I know of no way to extend the implementation in a similarly concise
way (for which the length of the solution does not depend on the
number of top level items in StdLabels). Have I overlooked anything


P.S.: FYI existing solution for the implementation:

  module String = struct
    include StringLabels

    let is_prefix_of (needle:string) (haystack:string) : bool =
      let len = length needle in
      len <= length haystack && (sub haystack ~pos:0 ~len = needle)

  (* Haven't quite found out how to include a module hiding another one... *)
  module List = ListLabels
  module Array = ArrayLabels