# Functors for library configuration

**URL:** <https://discuss.ocaml.org/t/functors-for-library-configuration/14974>\
**Category:** Learning\
**Tags:** functor, configuration\
**Created:** [July 15, 2024, 12:19pm UTC](https://discuss.ocaml.org/t/functors-for-library-configuration/14974 "2024-07-15T12:19:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fizzixnerd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fizzixnerd/32/5322_2.png) [@Fizzixnerd](https://discuss.ocaml.org/u/Fizzixnerd)\
**Post date:** [July 15, 2024, 12:19pm UTC](https://discuss.ocaml.org/t/functors-for-library-configuration/14974/1 "2024-07-15T12:19:08Z")

</div>

Hello,

I have a library that I’m working on, `living`, that I would like to add some configuration to. Specifically, `living` has a failsafe mechanism similar to that used by `Ctypes` for (specifically dynamic and not static) function pointers, whereby it requires you to free items explicitly and prevents the garbage collector from collecting the value if you don’t, to assure soundness. Like Ctypes, when this occurs, I print to stderr letting you know it happened.

However, I’d like to allow the user to pass their own function that is used for logging, and indeed _turn off_ this behaviour if they find it annoying or it is impacting performance too much.

To that end, I’ve functorized the main part of the library, where a user provides a module with the logging function and other configuration parameters, and the module returned acts accordingly.

See [this line](https://github.com/Fizzixnerd/ocaml-living/blob/main/lib/living_core.ml#L2) for the structure of a default config module, and  
[this function](https://github.com/Fizzixnerd/ocaml-living/blob/main/lib/living_core.ml#L35) for how it’s used.

I’m wondering if there is alternatives to this approach for passing user-defined behaviour to a module, or if there are otherwise other techniques I should know about here. One (slight) annoyance is that the module that ends up using this module needs to now be a functor too. I suppose this probably cannot be avoided, but I thought I’d ask.

---

<div class="post-metadata">

**Author:** ![emillon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emillon/32/1206_2.png) [@emillon](https://discuss.ocaml.org/u/emillon)\
**Post date:** [July 15, 2024, 12:58pm UTC](https://discuss.ocaml.org/t/functors-for-library-configuration/14974/2 "2024-07-15T12:58:28Z")

</div>

> [@Fizzixnerd](#):
>
> However, I’d like to allow the user to pass their own function that is used for logging, and indeed _turn off_ this behaviour if they find it annoying or it is impacting performance too much.

Some options:

- let users pass a callback that will be called whenever this event happens (you don’t have to pass it a formatted string)
- expose a global `ref` that will be used by your library
- use a logging library in a way that’s not enabled by default / can be disabled on a component per component basis (I think `logs` supports this)

---

<div class="post-metadata">

**Author:** ![Fizzixnerd](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/fizzixnerd/32/5322_2.png) [@Fizzixnerd](https://discuss.ocaml.org/u/Fizzixnerd)\
**Post date:** [July 15, 2024, 2:24pm UTC](https://discuss.ocaml.org/t/functors-for-library-configuration/14974/3 "2024-07-15T14:24:19Z")

</div>

Thanks, I didn’t think of using a logging library in that way. I think I’ll end up using the functor interface after all, though.
