Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Alain Frisch <alain@frisch.fr>
To: Tiphaine Turpin <Tiphaine.Turpin@irisa.fr>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Polymorphic values in local modules
Date: Wed, 03 Feb 2010 12:35:15 +0100	[thread overview]
Message-ID: <4B695F73.50803@frisch.fr> (raw)
In-Reply-To: <4B694A55.2040100@irisa.fr>

On 03/02/2010 11:05, Tiphaine Turpin wrote:
> It is possible however, provided you wrap the argument inside a
> polymorphic record field (or object) as follows:
>
> type t = {f : 'a. 'a ->  'a}
>
> let test f =
>   let module Foo : Foo_sig =
>       struct
>     let foo = f.f
>       end
>   in
>     ()

Another option (that will be available in OCaml 3.12) is to build the 
module as a first-class value outside the function "test".

=================================
module type Foo_sig = sig
   val f: 'a -> 'a
end

let test foo =
   let module Foo = (val foo : Foo_sig) in
   (* use the polymorphic function here... *)
   ()

let () =
   let foo_impl = (module struct let f a = a end : Foo_sig) in
   test foo_impl
=================================

The type for test is:

val test : (module Foo_sig) -> unit



Alain



      reply	other threads:[~2010-02-03 11:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-03 10:05 Tiphaine Turpin
2010-02-03 11:35 ` Alain Frisch [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B695F73.50803@frisch.fr \
    --to=alain@frisch.fr \
    --cc=Tiphaine.Turpin@irisa.fr \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox