| author | Alan Dipert
<alan@tailrecursion.com> 2026-11-12 13:50:20 UTC |
| committer | Alan Dipert
<alan@tailrecursion.com> 2026-11-12 13:50:20 UTC |
| parent | 3edf4de9d2f10e0b5275f3d0a9a0a1d3e724f488 |
| md/Lisp/MutableLocals.md | +2 | -0 |
diff --git a/md/Lisp/MutableLocals.md b/md/Lisp/MutableLocals.md index 61ffe26..4ba1de0 100644 --- a/md/Lisp/MutableLocals.md +++ b/md/Lisp/MutableLocals.md @@ -21,6 +21,8 @@ Here's an example using `loophole`, a macro provided by my Clojure library, [Bla ;; => 4 ``` +Clojure has [`with-local-vars`](https://clojure.github.io/clojure/clojure.core-api.html#clojure.core/with-local-vars) for mutable local state, but reads and writes look up thread-local Var bindings, and primitive numbers are boxed. I'd like to use ordinary JVM locals and keep primitive arithmetic where possible. + Clojure already updates [JVM local-variable slots](https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-2.html#jvms-2.6.1) to implement `loop` and `recur`. We can use the same mechanism for our own variables. [`recur`](https://clojure.org/reference/special_forms#recur) evaluates its arguments, rebinds the loop locals, and starts the loop body again. [Clojure's compiler](https://github.com/clojure/clojure/blob/clojure-1.12.6/src/jvm/clojure/lang/Compiler.java#L7156-L7227) emits local-variable stores and a jump. We can write assignments and jumps where we want them, then compile them into Clojure that does the updating through `recur`.