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.

Read more
Development

Implementing Dynamic Scoping in Fennel: A Clever Approach

2025-07-27
Implementing Dynamic Scoping in Fennel: A Clever Approach

The author tackles the challenge of implementing dynamic scoping in Fennel, a Lua dialect where it's not natively supported. The article explores several approaches, including manipulating function environments using Lua's debug library and cleverly cloning functions to set their environments. While the author ultimately decides against integrating this feature into the fennel-cljlib library for now, the in-depth exploration of Lua function environments and dynamic scoping, along with the comparison of different implementation strategies, provides valuable insights into functional and metaprogramming concepts.

Read more
Development Dynamic Scoping

Running Fennel from Emacs: A Powerful Extension

2025-07-23
Running Fennel from Emacs: A Powerful Extension

This article introduces `require-fennel.el`, an Emacs extension that enables running Fennel (a Lua dialect) within Emacs. It achieves this by communicating with a Fennel REPL, allowing data conversion and function calls between Emacs Lisp and Fennel. The author demonstrates loading Fennel modules, calling Fennel functions, and using Fennel data structures in Emacs Lisp. Furthermore, the extension supports calling Emacs Lisp functions from Fennel, enabling two-way interaction. This allows developers to leverage Fennel's conciseness and Emacs's power for a more robust Emacs environment.

Read more
Development