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

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 index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-30 13:11 Miguel Ambrona
2019-04-30 13:28 ` Nicolás Ojeda Bär [this message]
```

```Reply instructions:

You may reply publically 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,

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

switches of git-send-email(1):

git send-email \
--to=nicolas.ojeda.bar@lexifi.com \
--cc=caml-list@inria.fr \
--cc=miguel.ambrona@imdea.org \
```caml-list - the Caml user's mailing list