Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What are you trying to do exactly? Do you want your function to be parametrized over an ad <em>type</em> (eg. <code>Ads.T.t</code>), or over an ad <em>module</em> (eg. <code>Ads.T</code>) ?</p> <p>In both cases, you should wrap those generic functions in modules:</p> <pre><code>module Generic (Ad : Ads.T) : sig val search : int -&gt; Ad.t -&gt; int list end = struct let search _ _ = assert false end </code></pre> <p>You can then instantiate them easily, eg. to work with your <code>Conrete_ads</code> modules:</p> <pre><code>module AT = Concrete_ads.make(struct ... end) module Lib = Generic(AT) let foo = Lib.search 2 (AT.create ()) </code></pre> <p>Of course, if you would just like your functions to be parametrized over a specific, concrete type:</p> <pre><code>val search : int -&gt; AT.t -&gt; int list </code></pre> <p>PS: in your definition of <code>AT</code>, you forgot the <code>struct</code> in the <code>struct .. end</code> module argument of the functor.</p> <p>PPS: with OCaml 3.12 there is a new shiny thing called <a href="http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc81" rel="nofollow">first-class modules</a> that allows to pass a module as a value argument to a function</p> <pre><code>val search : int -&gt; (module Ad.T) -&gt; int list let search n ad_module = let module Ad = (val ad_module : Ad.T) in ... Ad.foo .. ... search 2 (module AT : Ad.T) ... </code></pre> <p>(Explanation : <code>module S</code>, as a type expression, is the type of values that are "reified modules" of signature <code>S</code>; <code>(val t : S)</code>, as a module expression, is the module that was packed into the value <code>t</code>, with signature <code>S</code>. Here I take <code>ad_module</code> as a value and unpack it into the <code>Ad</code> module locally, which can then be used as any other module inside the function. Finally, <code>(module M : S)</code> is a term expression that packs the module <code>M</code> with signature <code>S</code> into a value.)</p> <p>It can supplement using a functor in some cases, but as it is new, a bit more complex (there are non-trivial limitations on the use of first-class modules) and possibly going to change a bit in the next language versions, I would advise keeping the tried-and-true functor construction.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload