caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Lightweight way to extend existing module. Almost there?
@ 2010-11-17  4:10 Till Varoquaux
  0 siblings, 0 replies; only message in thread
From: Till Varoquaux @ 2010-11-17  4:10 UTC (permalink / raw)
  To: Caml Mailing List

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
  end

  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
obvious?

Till

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)
  end

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-11-17  4:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-17  4:10 Lightweight way to extend existing module. Almost there? Till Varoquaux

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).