caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Function that outputs a functor
@ 2019-04-30 13:10 Miguel Ambrona
  2019-04-30 13:27 ` Nicolás Ojeda Bär
  0 siblings, 1 reply; 2+ messages in thread
From: Miguel Ambrona @ 2019-04-30 13:10 UTC (permalink / raw)
  To: caml-list

Dear caml-hackers,

Is there a way of writing a function that returns a functor?

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Function that outputs a functor
  2019-04-30 13:10 [Caml-list] Function that outputs a functor Miguel Ambrona
@ 2019-04-30 13:27 ` Nicolás Ojeda Bär
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolás Ojeda Bär @ 2019-04-30 13:27 UTC (permalink / raw)
  To: Miguel Ambrona; +Cc: OCaml Mailing List

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
>
>
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-30 13:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-30 13:10 [Caml-list] Function that outputs a functor Miguel Ambrona
2019-04-30 13:27 ` Nicolás Ojeda Bär

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).