caml-list - the Caml user's mailing list
 help / Atom feed
* [Caml-list] Function that outputs a functor
@ 2019-04-30 13:11 Miguel Ambrona
  2019-04-30 13:28 ` Nicolás Ojeda Bär
  0 siblings, 1 reply; 2+ messages in thread
From: Miguel Ambrona @ 2019-04-30 13:11 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:11 [Caml-list] Function that outputs a functor Miguel Ambrona
@ 2019-04-30 13:28 ` Nicolás Ojeda Bär
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolás Ojeda Bär @ 2019-04-30 13:28 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, back to index

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

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox