caml-list - the Caml user's mailing list
 help / Atom feed
* [Caml-list] How to shadow nested submodules?
@ 2019-10-11 15:24 Vadim
  2019-10-11 15:40 ` Thierry Martinez
  0 siblings, 1 reply; 3+ messages in thread
From: Vadim @ 2019-10-11 15:24 UTC (permalink / raw)
  To: caml-list

Hello! I'm trying to figure out how shadow/override nested modules in 
OCaml. For example, I want to make ipaddr lib aware of 
ppx_deriving_yojson, so I'm trying something like:


> module type IpaddrS = module type of Ipaddr
> module Ipaddr = struct
>   include (Ipaddr : IpaddrS)
>   let to_yojson t = `String (to_string t)
>   let of_yojson = function
>     | `String s -> begin match of_string s with
>         | Error (`Msg e) -> Error e
>         | Ok r -> Ok r
>       end
>     | _ -> Error "Expected string"
>   module Prefix = struct
>     include Ipaddr.Prefix
>     let to_yojson t = `String (to_string t)
>     let of_yojson = function
>       | `String s -> begin match of_string s with
>                      | Error (`Msg e) -> Error e
>                      | Ok r -> Ok r
>                      end
>       | _ -> Error "Expected string"
>   end
> end
>
> type t = {
>     addr: Ipaddr.t;
>     net: Ipaddr.Prefix.t;
>   } [@@deriving yojson]
but it fails with
> Error: Multiple definition of the module name Prefix.
>        Names must be unique in a given structure or signature.
because `include (Ipaddr : IpaddrS)` has already added it. I can handle 
it with copy and pasting the whole signature of Ipaddr module (`module 
type IpaddrS = sig ...end`) but its too big.
Is there any ways to do it without copypasting?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How to shadow nested submodules?
  2019-10-11 15:24 [Caml-list] How to shadow nested submodules? Vadim
@ 2019-10-11 15:40 ` Thierry Martinez
  2019-10-11 16:09   ` Vadim
  0 siblings, 1 reply; 3+ messages in thread
From: Thierry Martinez @ 2019-10-11 15:40 UTC (permalink / raw)
  To: Vadim; +Cc: caml-list

Hello, Vadim!

Vadim:
> Hello! I'm trying to figure out how shadow/override nested modules in
> OCaml.

A generic solution for old OCaml versions (<4.08) has been explained by
Gabriel Scherer in the following blog post:
http://gallium.inria.fr/blog/overriding-submodules/

The idea is to use destructive substitution while including the original
Ipaddr:
http://caml.inria.fr/pub/docs/manual-ocaml-4.01/extn.html#sec234

module Ipaddr = struct
  include (Ipaddr : module type of Ipaddr with module Prefix := Ipaddr.Prefix)
  ...
end

But I guess that the code you posted should work just fine with OCaml >=4.08
thanks to the following change:
https://github.com/ocaml/ocaml/pull/1892

Best regards.
-- 
Thierry Martinez.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] How to shadow nested submodules?
  2019-10-11 15:40 ` Thierry Martinez
@ 2019-10-11 16:09   ` Vadim
  0 siblings, 0 replies; 3+ messages in thread
From: Vadim @ 2019-10-11 16:09 UTC (permalink / raw)
  To: Thierry Martinez; +Cc: caml-list

[-- Attachment #0: Type: text/html, Size: 1447 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-11 15:24 [Caml-list] How to shadow nested submodules? Vadim
2019-10-11 15:40 ` Thierry Martinez
2019-10-11 16:09   ` Vadim

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox