# \#first-class\_modules

**URL:** https://discuss.ocaml.org/tag/first-class-modules/140.md

[Latest](https://discuss.ocaml.org/latest.md) · [Categories](https://discuss.ocaml.org/categories.md) · [Tags](https://discuss.ocaml.org/tags.md)

---

## [How to achieve implicit state without using classes and objects](https://discuss.ocaml.org/t/how-to-achieve-implicit-state-without-using-classes-and-objects/18582)

<div class="topic-metadata">

**Author:** [@rvdalen](https://discuss.ocaml.org/u/rvdalen)\
**Replies:** 6\
**Last updated:** [October 20, 2026, 5:49am UTC](https://discuss.ocaml.org/t/how-to-achieve-implicit-state-without-using-classes-and-objects/18582 "2026-10-20T05:49:26Z")

</div>

Hi everyone, I am new to OCaml and I am trying to write my own parser library just for fun. The easiest way to start of course is explicitly passing the updated parser state around. For example: let my\_parser (st: To…

---

## [Q: module dependent class bindings?](https://discuss.ocaml.org/t/q-module-dependent-class-bindings/18581)

<div class="topic-metadata">

**Author:** [@jhw](https://discuss.ocaml.org/u/jhw)\
**Replies:** 9\
**Last updated:** [October 6, 2026, 10:59pm UTC](https://discuss.ocaml.org/t/q-module-dependent-class-bindings/18581 "2026-10-06T22:59:02Z")

</div>

Messing around with module dependent functions, I discovered that class bindings in OCaml 5.5 cannot be module dependent. To illustrate (does not compile): module type S = sig type t val show: t -\> string end module N =…

---

## [Modular explicit forms (as, type parameters)](https://discuss.ocaml.org/t/modular-explicit-forms-as-type-parameters/18511)

<div class="topic-metadata">

**Author:** [@smuenzel](https://discuss.ocaml.org/u/smuenzel)\
**Replies:** 1\
**Last updated:** [September 6, 2026, 6:46pm UTC](https://discuss.ocaml.org/t/modular-explicit-forms-as-type-parameters/18511 "2026-09-06T18:46:15Z")

</div>

Are the following forms for modular explicit planned to be supported? val x : ((module A : A)) -\> unit val x' : ((module A : A) as 'a) -\> 'a t -\> unit val x'' : (module A : A) t -\> A.t -\> unit Right now, it seems that …

---

## [Module type constraints in first class modules](https://discuss.ocaml.org/t/module-type-constraints-in-first-class-modules/16155)

<div class="topic-metadata">

**Author:** [@dlesbre](https://discuss.ocaml.org/u/dlesbre)\
**Replies:** 2\
**Last updated:** [February 28, 2025, 8:59pm UTC](https://discuss.ocaml.org/t/module-type-constraints-in-first-class-modules/16155 "2025-02-28T20:59:59Z")

</div>

I’ve stumbled into a weird situation recently when using first-class modules. For context, I have to functors with the same type, and I would like to choose one dynamically at run time: module type BAR = sig type 'a t e…

---

## [Figuring out how to return an open polymorphic variant from application to a module](https://discuss.ocaml.org/t/figuring-out-how-to-return-an-open-polymorphic-variant-from-application-to-a-module/15292)

<div class="topic-metadata">

**Author:** [@s.t.s](https://discuss.ocaml.org/u/s.t.s)\
**Replies:** 2\
**Last updated:** [September 18, 2024, 12:45am UTC](https://discuss.ocaml.org/t/figuring-out-how-to-return-an-open-polymorphic-variant-from-application-to-a-module/15292 "2024-09-18T00:45:03Z")

</div>

I stumbled upon an interesting problem when attempting to figure out a way to nicely and elegantly encode tags for data in a modular fashion. First, we may want to approach the problem in an elementary fashion: module …

---

## [FCM and modular implicits type system](https://discuss.ocaml.org/t/fcm-and-modular-implicits-type-system/7085)

<div class="topic-metadata">

**Author:** [@EduardoRFS](https://discuss.ocaml.org/u/EduardoRFS)\
**Replies:** 6\
**Last updated:** [July 2, 2024, 12:52pm UTC](https://discuss.ocaml.org/t/fcm-and-modular-implicits-type-system/7085 "2024-07-02T12:52:14Z")

</div>

I was reading the implicits paper again and playing with the explicits PR, but I couldn’t find a good answer for a question. Why implicits require a new way to deal with modules, besides of the syntax improvement? Can’t …

---

## [Reducing duplicate code in modules with parameterized type](https://discuss.ocaml.org/t/reducing-duplicate-code-in-modules-with-parameterized-type/13446)

<div class="topic-metadata">

**Author:** [@cpa](https://discuss.ocaml.org/u/cpa)\
**Replies:** 3\
**Last updated:** [November 18, 2023, 4:25am UTC](https://discuss.ocaml.org/t/reducing-duplicate-code-in-modules-with-parameterized-type/13446 "2023-11-18T04:25:18Z")

</div>

Hello, I found myself writing the following piece of code, and I’m wondering how can I reuse more code and not repeat re-declaring type expr = ... in both modules (since outside of this minimal example this would be a l…

---

## [Idiomatic group\_by/distinct\_by/count\_by](https://discuss.ocaml.org/t/idiomatic-group-by-distinct-by-count-by/9403)

<div class="topic-metadata">

**Author:** [@Jon\_Harrop](https://discuss.ocaml.org/u/Jon_Harrop)\
**Replies:** 12\
**Last updated:** [October 25, 2023, 12:52pm UTC](https://discuss.ocaml.org/t/idiomatic-group-by-distinct-by-count-by/9403 "2023-10-25T12:52:16Z")

</div>

I love using pipelines of combinators to munge data but OCaml’s stdlib is missing some obvious ones such as group\_by/distinct\_by/count\_by. You can write them easily enough in vanilla OCaml using Hashtbl, e.g. Array.disti…

---

## [What's the runtime representation of first-class modules?](https://discuss.ocaml.org/t/whats-the-runtime-representation-of-first-class-modules/12074)

<div class="topic-metadata">

**Author:** [@nated](https://discuss.ocaml.org/u/nated)\
**Replies:** 5\
**Last updated:** [May 3, 2023, 1:18am UTC](https://discuss.ocaml.org/t/whats-the-runtime-representation-of-first-class-modules/12074 "2023-05-03T01:18:59Z")

</div>

As in something like let f (module M : MOD) = M.f "neat" Just trying to get an intuition for what this is like at runtime.

---

## [Self-referential first-class modules in functor signature](https://discuss.ocaml.org/t/self-referential-first-class-modules-in-functor-signature/10906)

<div class="topic-metadata">

**Author:** [@andrepd](https://discuss.ocaml.org/u/andrepd)\
**Replies:** 1\
**Last updated:** [December 3, 2022, 6:57pm UTC](https://discuss.ocaml.org/t/self-referential-first-class-modules-in-functor-signature/10906 "2022-12-03T18:57:55Z")

</div>

How can I have a module signature where one of the values accepts a first-class module of that very same signature? MWE: module type SIG = sig type t val size : int val convert : (module SIG) -\> t -\> P.t option end …

---

## [Type of parameterized class is not bound within methods](https://discuss.ocaml.org/t/type-of-parameterized-class-is-not-bound-within-methods/9618)

<div class="topic-metadata">

**Author:** [@stw](https://discuss.ocaml.org/u/stw)\
**Replies:** 4\
**Last updated:** [April 3, 2022, 8:45pm UTC](https://discuss.ocaml.org/t/type-of-parameterized-class-is-not-bound-within-methods/9618 "2022-04-03T20:45:02Z")

</div>

Defining a local exception or module using the type of the parameterized class is not possible as the type is not bound. See this minimal example: class \['a\] foo = object method bar : unit = (\* Neither of th…

---

## [First class module signature mismatch](https://discuss.ocaml.org/t/first-class-module-signature-mismatch/9051)

<div class="topic-metadata">

**Author:** [@VPhantom](https://discuss.ocaml.org/u/VPhantom)\
**Replies:** 3\
**Last updated:** [December 28, 2021, 2:36pm UTC](https://discuss.ocaml.org/t/first-class-module-signature-mismatch/9051 "2021-12-28T14:36:50Z")

</div>

I’m stuck… I’m experimenting with a cute way to abstract string/bytes/Buffer.t/bigstring/etc. reading/writing in a single interface, and the very last step which I thought would be nice, is to use a functor to “curry” t…

---

## [Confused about aliases of first class module arguments](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695)

<div class="topic-metadata">

**Author:** [@jfeser](https://discuss.ocaml.org/u/jfeser)\
**Replies:** 10\
**Last updated:** [November 10, 2021, 4:46pm UTC](https://discuss.ocaml.org/t/confused-about-aliases-of-first-class-module-arguments/8695 "2021-11-10T16:46:44Z")

</div>

I ran into the following example, and I was surprised to find that f' is accepted while f is not. Is this expected behavior? I’m guessing it’s related to module type inference on (module B)—but I thought that module type…

---

## [How to type a forwarding function?](https://discuss.ocaml.org/t/how-to-type-a-forwarding-function/7939)

<div class="topic-metadata">

**Author:** [@orbitz](https://discuss.ocaml.org/u/orbitz)\
**Replies:** 2\
**Last updated:** [May 30, 2021, 7:37pm UTC](https://discuss.ocaml.org/t/how-to-type-a-forwarding-function/7939 "2021-05-30T19:37:03Z")

</div>

I’m not sure if below is the minimal code example, but what I’m trying to accomplish is the function k below, which takes a function and a first-class module, and the function is just a forwarding function for something …

---

## [Problems with type arity](https://discuss.ocaml.org/t/problems-with-type-arity/5881)

<div class="topic-metadata">

**Author:** [@onetimerobot](https://discuss.ocaml.org/u/onetimerobot)\
**Replies:** 1\
**Last updated:** [June 1, 2020, 11:21am UTC](https://discuss.ocaml.org/t/problems-with-type-arity/5881 "2020-06-01T11:21:30Z")

</div>

So I’m trying to write a function which takes as an argument any module that implements certain functions. The real code is more complex, but this is a minimal example that illustrates the problem: module type Len\_intf …

---

## [Can you factorize this code more?](https://discuss.ocaml.org/t/can-you-factorize-this-code-more/5772)

<div class="topic-metadata">

**Author:** [@UnixJunkie](https://discuss.ocaml.org/u/UnixJunkie)\
**Replies:** 13\
**Last updated:** [May 15, 2020, 2:29pm UTC](https://discuss.ocaml.org/t/can-you-factorize-this-code-more/5772 "2020-05-15T14:29:10Z")

</div>

I managed to get what I want to compile. However, there is a big copy-paste if preserve then X else Y in the run function. X and Y are almost the same, except that X uses module Ord while Y uses the module Dis. The …

---

## [Single-function functors or first-class modules in function arguments?](https://discuss.ocaml.org/t/single-function-functors-or-first-class-modules-in-function-arguments/4792)

<div class="topic-metadata">

**Author:** [@MattWindsor91](https://discuss.ocaml.org/u/MattWindsor91)\
**Replies:** 0\
**Last updated:** [November 28, 2019, 3:16pm UTC](https://discuss.ocaml.org/t/single-function-functors-or-first-class-modules-in-function-arguments/4792 "2019-11-28T15:16:35Z")

</div>

Hi all, Suppose I want to make a function that takes a module parameter. I can think of two ways to do it: either I wrap the function inside a functor: module On\_monad (M : Monad.S) = struct let map\_m x ~f = (\* so…

---

## [Forget type constraint in function returning first-class module](https://discuss.ocaml.org/t/forget-type-constraint-in-function-returning-first-class-module/4729)

<div class="topic-metadata">

**Author:** [@nico\_toll](https://discuss.ocaml.org/u/nico_toll)\
**Replies:** 2\
**Last updated:** [November 19, 2019, 8:02pm UTC](https://discuss.ocaml.org/t/forget-type-constraint-in-function-returning-first-class-module/4729 "2019-11-19T20:02:23Z")

</div>

Hi folks, I am a bit stuck. If I declare these toy modules and functors: module type Empty = sig end module type Type = sig type t end module F(E:Empty): Type with type t = int = struct type t = int end let f (module…

---

## [Why parametrized types are not supported in (module package-type)?](https://discuss.ocaml.org/t/why-parametrized-types-are-not-supported-in-module-package-type/3115)

<div class="topic-metadata">

**Author:** [@noti0na1](https://discuss.ocaml.org/u/noti0na1)\
**Replies:** 2\
**Last updated:** [December 30, 2018, 8:55pm UTC](https://discuss.ocaml.org/t/why-parametrized-types-are-not-supported-in-module-package-type/3115 "2018-12-30T20:55:41Z")

</div>

Let’s say we have a module type M with the following signature: module type M = sig type 'a t val x : 'a t end and a module ListM: module ListM : (M with type 'a t = 'a list) = struct type 'a t = 'a list …

---

## [First class module and existential type](https://discuss.ocaml.org/t/first-class-module-and-existential-type/1253)

<div class="topic-metadata">

**Author:** [@fdagnat](https://discuss.ocaml.org/u/fdagnat)\
**Replies:** 7\
**Last updated:** [December 25, 2017, 11:28pm UTC](https://discuss.ocaml.org/t/first-class-module-and-existential-type/1253 "2017-12-25T23:28:09Z")

</div>

After being lazy for some years, I decided to get a grasp of first class modules. So far so good but I’m currently stuck within my experiments mainly in relation to the way one can add typing constraints to such module v…
