caml-list - the Caml user's mailing list
 help / color / 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

Wow! Thank you very much! I don't know why I could not find it.<br /><br />18:40, 11 октября 2019 г., Thierry Martinez &lt;thierry.martinez@inria.fr&gt;:<br /><blockquote class="b4fd5cf2ec92bc68cb898700bb81355fwmi-quote"><p>Hello, Vadim!<br /><br />Vadim:<br /></p><blockquote class="b4fd5cf2ec92bc68cb898700bb81355fwmi-quote"> Hello! I'm trying to figure out how shadow/override nested modules in<br /> OCaml.<br /></blockquote><p><br />A generic solution for old OCaml versions (&lt;4.08) has been explained by<br />Gabriel Scherer in the following blog post:<br /><a href="http://gallium.inria.fr/blog/overriding-submodules/">http://gallium.inria.fr/blog/overriding-submodules/</a><br /><br />The idea is to use destructive substitution while including the original<br />Ipaddr:<br /><a href="http://caml.inria.fr/pub/docs/manual-ocaml-4.01/extn.html#sec234">http://caml.inria.fr/pub/docs/manual-ocaml-4.01/extn.html#sec234</a><br /><br />module Ipaddr = struct<br />  include (Ipaddr : module type of Ipaddr with module Prefix := Ipaddr.Prefix)<br />  ...<br />end<br /><br />But I guess that the code you posted should work just fine with OCaml &gt;=4.08<br />thanks to the following change:<br /><a href="https://github.com/ocaml/ocaml/pull/1892">https://github.com/ocaml/ocaml/pull/1892</a><br /><br />Best regards.<br /></p><span class="c18e9d485856a85513717a5a5b59d3fewmi-sign">-- <br />Thierry Martinez.</span></blockquote>

^ 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 http://inbox.vuxu.org/caml-list
	git clone --mirror https://inbox.ocaml.org/caml-list

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.caml-list


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