Skip to content

Use setMacro to flag macros in the api namespace#185

Merged
carloshernandez2 merged 1 commit intomasterfrom
use-setmacro-to-flag-macros-in-api
Mar 6, 2025
Merged

Use setMacro to flag macros in the api namespace#185
carloshernandez2 merged 1 commit intomasterfrom
use-setmacro-to-flag-macros-in-api

Conversation

@carloshernandez2
Copy link
Contributor

@carloshernandez2 carloshernandez2 commented Mar 5, 2025

Problem

It is not possible to AOT-compile namespaces that use the defflow macro from the API namespace and get a faster development workflow as described here.

Description

Taking these namespaces as a starting point:

(ns death-by-compilation-clj.my-defflow
  (:require [clojure.test :refer :all]))

(defmacro defflow
  [name]
  `(def ~name 1))
(ns death-by-compilation-clj.my-dear-defflow
  (:require [clojure.test :refer :all]
            [death-by-compilation-clj.my-defflow :as my-defflow]))

(def ^{:macro    true}
  defflow #'my-defflow/defflow)
(ns death-by-compilation-clj.my-dear-test
  (:require [clojure.test :refer :all]
            [death-by-compilation-clj.my-dear-defflow :refer [defflow]]))

(defflow my-dear-test-test)

When running compile on death-by-compilation-clj.my-dear-test it successfully compiles and writes to disk. Then if I kill the REPL, modify the file and repeat the process, the following error is thrown:

Syntax error compiling at (death_by_compilation_clj/my_dear_test.clj:7:1).
Unable to resolve symbol: my-dear-test-test in this context

Solution

By calling the setMacro method on the Vars that correspond to macros in the API namespace, we can flag them as the compiler expects and also add the macro metadata.

References

@carloshernandez2 carloshernandez2 marked this pull request as ready for review March 5, 2025 20:55
@carloshernandez2 carloshernandez2 requested a review from a team as a code owner March 5, 2025 20:55
It also sets the meta value which was the only thing that was being done
before
@carloshernandez2 carloshernandez2 force-pushed the use-setmacro-to-flag-macros-in-api branch from 7d7fb30 to 901fddc Compare March 5, 2025 21:06
Copy link

@acamargo acamargo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@carloshernandez2 carloshernandez2 merged commit 9ba2810 into master Mar 6, 2025
5 of 6 checks passed
@carloshernandez2 carloshernandez2 deleted the use-setmacro-to-flag-macros-in-api branch March 6, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants