The Challenges and Solutions of Single-File Fennel Libraries

2025-08-13
The Challenges and Solutions of Single-File Fennel Libraries

This article delves into the challenges of building single-file libraries in Fennel, a Lisp dialect embedded in Lua, particularly the complexities of handling macros and functions together. The author meticulously dissects Lua's module system, including the mechanics of `package.loaded`, `package.preload`, and `package.searchers`, explaining how Fennel leverages them for compiling and loading modules. The core of the article focuses on resolving limitations of Fennel macros, such as the inability to directly export macros and the interdependence between macros. A clever solution is presented, utilizing `eval-compiler` and `relative-require` to package macros and functions within a single file, and addressing macro loading by manually setting the `fennel.macro-loaded` table during compilation. Finally, the author outlines future improvements for Fennel's macro system, proposing the removal of macro modules, direct loading of entire modules during compilation, and adopting Clojure's approach to resolve macro dependencies.

Development