Skip to content

Commit db0dbdc

Browse files
committed
Recursion in require-modules / requires / needs
1 parent 467d0df commit db0dbdc

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,23 @@ The reason for this behaviour is that the file located is often a tiny 'loader'
165165
**`require-modules`** is a shim around `require-module` which expects a list of module descriptions instead of a module name. It takes all the same keyword arguments as `require-module`. A *module description* is either
166166

167167
- a module name;
168-
- or a cons of a module name and some arguments to `require-modules`.
168+
- or a cons of a module description and some arguments to `require-modules`.
169169

170-
In the second case `require-module` is called with the two sets of arguments appended to each other, with those from the module specification first. Because CL uses the first of any repeated keyword arguments, this means that individual module specifications can override the keyword arguments provided to the function as a whole. `require-modules` returns a list of lists of the two values returned by each `require-module` it calls.
170+
In the second case either `require-module` (if the nested description is not itself a cons) or `require-modules` is called with the two sets of arguments appended to each other, with those from the module specification first. Because CL uses the first of any repeated keyword arguments, this means that individual module specifications can override the keyword arguments provided to the function as a whole. `require-modules` returns a list of lists of the two values returned by each `require-module` it calls.
171171

172172
**`requires`** is a NOSPREAD version of `require-modules` with a fallback to `require`. So `requires` lets you say that you want one or more modules, and if `require-module` doesn't know how to get them then the system should try `require` in case it does.
173173

174174
**`needs`** lets you express a dependency on modules at compile time: it is `requires` wrapped in a suitable `eval-when`, but its arguments are quoted. Note that `needs` quotes its arguments: an older version didn't so this is an incompatible change. If you use strings or keywords as module names this doesn't matter, but it makes things like `(needs (:org.tfeb.hax.collecting :use t))` more natural[^8].
175175

176+
The recursive behaviour of `require-modules` is inherited by both `requires` and `needs`, and lets you say things like this:
177+
178+
```lisp
179+
(needs ((:org.tfeb.hax.collecting :org.tfeb.hax.iterate)
180+
:compile t :use t))
181+
```
182+
183+
which is sometimes convenient.
184+
176185
**`clear-module-caches`** is a function of no arguments which will clear both the cache of loaded files & their write dates and the structure (not really a cache) which maintains the notion of the descendants of a module.
177186

178187
### The search list

require-module.lisp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,13 +696,19 @@
696696
;; Require a list of module specifications. Return a list of list
697697
;; of the values returned by REQUIRE-MODULE for each module. A
698698
;; module specification is either the name of a module or a cons of
699-
;; (module name . keyword-arguments)
699+
;; a module specification and some default keyword arguments (this
700+
;; is recursive).
700701
(loop for m in module-specifications
701702
collect (multiple-value-list
702-
(if (atom m)
703-
(apply #'require-module m keywords)
704-
(apply #'require-module (first m)
705-
(append (rest m) keywords))))))
703+
(typecase m
704+
(cons
705+
(destructuring-bind (specification/s . defaults) m
706+
(apply (typecase specification/s
707+
(cons #'require-modules)
708+
(t #'require-module))
709+
(append defaults keywords))))
710+
(t
711+
(apply #'require-module m keywords))))))
706712

707713
(defun requires (&rest module-specifications)
708714
;; NOSPREAD version of require-modules, with a fallback to REQUIRE

0 commit comments

Comments
 (0)