From: "Nicolás Ojeda Bär" <nicolas.ojeda.bar@lexifi.com>
To: Miguel Ambrona <miguel.ambrona@imdea.org>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Function that outputs a functor
Date: Tue, 30 Apr 2019 13:27:37 +0000 [thread overview]
Message-ID: <CADK7aFMjySSSkBNbm7tAgmNiO3GEDV-Cjh3gSQP7Wk0Dh8DttA@mail.gmail.com> (raw)
In-Reply-To: <f5de4440-983b-a51f-0093-c292b7b674fa@imdea.org>
Hi Miguel,
On Tue, Apr 30, 2019 at 3:11 PM Miguel Ambrona <miguel.ambrona@imdea.org> wrote:
>
> Dear caml-hackers,
>
> Is there a way of writing a function that returns a functor?
Yes, you can do this along the same lines of your example:
```
# module type X = sig val m : int end;;
module type X = sig val m : int end
# module type U = functor (X : X) -> sig val total: int end;;
module type U = functor (X : X) -> sig val total : int end
# let make (n : int) = (module (functor (X : X) -> struct let total =
n + X.m end) : U);;
val make : int -> (module U) = <fun>
```
You can then use your function as follows:
```
# let module U = (val (make 7) : U) in let module U = U (struct let m
= 12 end) in U.total;;
- : int = 19
```
Cheers!
--
Nicolás OJEDA BÄR
nicolas.ojeda.bar@lexifi.com
https://www.lexifi.com
> For example, the following function takes an integer and returns a module:
>
> ``let make (n : int) = (module struct blablabla end : MyModule)''
>
> Can I do the same with a functor as output instead of a module? Of
> course, the implementation may use the integer "n".
>
> Thanks a lot!
>
> Best,
>
> Miguel
>
>
>
prev parent reply other threads:[~2019-04-30 13:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-30 13:10 Miguel Ambrona
2019-04-30 13:27 ` Nicolás Ojeda Bär [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=CADK7aFMjySSSkBNbm7tAgmNiO3GEDV-Cjh3gSQP7Wk0Dh8DttA@mail.gmail.com \
--to=nicolas.ojeda.bar@lexifi.com \
--cc=caml-list@inria.fr \
--cc=miguel.ambrona@imdea.org \
/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