# Getting all available submodules in a module

**URL:** <https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542>\
**Category:** Questions & Answers\
**Tags:** module\
**Created:** [February 5, 2025, 6:35pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542 "2025-02-05T18:35:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@nik](https://racket.discourse.group/u/nik)\
**Post date:** [February 5, 2025, 6:35pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542/1 "2025-02-05T18:35:55Z")

</div>

I have a module wherein I defined several submodules. Is there an easy way to get a list of all of the submodules? If I have the following file, `example.rkt`

```racket
#lang racket/base

(module one racket/base
  (provide doc)
  (define doc "Hello from here."))

(module two racket/base
  (provide doc)
  (define doc "Hello from here, too."))

```

Then I can dynamic-require the submodules individually with `(dynamic-require '(submod "example1.rkt" one) 'doc)`. What I would like to do is essentially be able to map over all submodules, requiring a variable that is known to be defined (would be even better if I can somehow test if it actually is defined).

I feel like there should be a somewhat elegant solution, but I haven't found anything in that direction

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [February 5, 2025, 9:04pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542/2 "2025-02-05T21:04:36Z")

</div>

A bit of a hack?

Bind a runtime variable to the (compile-time) result of syntax-local-submodules, then use the failure-thunk for when the binding you want isn't available.

Though I doubt this will cooperate well with raco exe/dist/demod, so if you plan to use those you might end up needing to maintain a static list of "dynamically required submodules" anyway.

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [February 5, 2025, 10:49pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542/3 "2025-02-05T22:49:00Z")

</div>

Needs some dynamic requiring here 😃

Moreover, I think in this code `SRC.rkt` can't be already compiled (to `.zo`).

```racket
#lang racket/base

(define old-compile (current-compile))

(define compiled-mod #f)

(parameterize ([current-compile (lambda (form imm-use?)
                                  (define compiled-res (old-compile form imm-use?))
                                  (define name (current-module-declare-name))
                                  (when (and (resolved-module-path? name)
                                             (regexp-match? #rx"SRC[.]rkt"
                                                            (path->string
                                                             (resolved-module-path-name
                                                              name))))
                                    (set! compiled-mod compiled-res))
                                  compiled-res)])
  (dynamic-require "SRC.rkt" #f))

'module
(map module-compiled-name (module-compiled-submodules compiled-mod #f))

'module*
(map module-compiled-name (module-compiled-submodules compiled-mod #t))

```

---

<div class="post-metadata">

**Author:** ![nik](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@nik](https://racket.discourse.group/u/nik)\
**Post date:** [February 5, 2025, 11:29pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542/4 "2025-02-05T23:29:46Z")

</div>

Cool, that looks pretty good! One surprising thing is that this also lists a submodule with the name `configure-runtime` in the modules. I actually don't know where that is coming from, it's a bit surprising to me to see it show up. Though I guess that should be reasonably easy to filter out.

(And a really small nitpick: the order of `'module` and `'module*` is swapped.)

---

<div class="post-metadata">

**Author:** ![shhyou](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@shhyou](https://racket.discourse.group/u/shhyou)\
**Post date:** [February 5, 2025, 11:46pm UTC](https://racket.discourse.group/t/getting-all-available-submodules-in-a-module/3542/5 "2025-02-05T23:46:47Z")

</div>

`configure-runtime` is added by `#lang racket[/base]` [3.1&nbsp;Modules: module, module\*, ...](https://docs.racket-lang.org/reference/module.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._~23~25module-begin%29%29).
