clojure

2026-02-22T13:56:25.608989Z

Is it possible to use requiring-resolve for macros ? It seems to give me something using it on a macro, but a "fn"...

bronsa 2026-02-22T13:58:28.009059Z

requiring-resolve is a runtime operation, macros are compile time

bronsa 2026-02-22T13:58:38.977579Z

you can't resolve a compile time operation at runtime and expect it to work

2026-02-22T14:00:07.148429Z

I though so. So if I have an "optional library" which I don't want in my deps.edn, I can not use requiring-resolve for any macro in it ?

bronsa 2026-02-22T14:03:30.378649Z

well, not with requiring-resolve,you could write a macro to kinda do what you need

bronsa 2026-02-22T14:04:07.099269Z

something like

(defmacro when-requiring-resolve-macro [sym & body] (when (try (-> sym requiring-resolve meta :macro) (catch Exception _ false)) `(do ~@body)))

bronsa 2026-02-22T14:04:16.502889Z

(when-requiring-resolve-macro lib/my-macro ..body using lib/my.macro..)

bronsa 2026-02-22T14:08:09.815079Z

user=> (ns foo)
nil
foo=> (defmacro x [y] `(println '~&form ~y))
#'foo/x
foo=> (in-ns 'user)
#object[clojure.lang.Namespace 0x3005db4a "user"]
user=> (defmacro when-requiring-resolve-macro [sym & body] (when (try (-> sym requiring-resolve meta :macro) (catch Exception _ false)) `(do ~@body)))
#'user/when-requiring-resolve-macro
user=> (defn a [] (when-requiring-resolve-macro foo/x (foo/x (println "foo"))))
#'user/a
user=> (defn b [] (when-requiring-resolve-macro does-not/exist (does-not/exist (println "foo"))))
#'user/b
user=> (a)
foo
(foo/x (println foo)) nil
nil
user=> (b)
nil

2026-02-22T14:15:55.627469Z

Belo does not compile, when "carmine" is not in classpath.

taoensso.carmine/wcar
is the macro I want to use
(reset! ml/train-predict-cache {:use-cache true
                                  :get-fn (fn [key] 
                                            (with-requiring-resolve-macro taoensso.carmine/wcar
                                              (taoensso.carmine/wcar
                                               wcar-opts
                                               ((requiring-resolve 'taoensso.carmine/get) key))))
                                  
                                  :set-fn (fn [key value]
                                            (with-requiring-resolve-macro taoensso.carmine/wcar
                                              (taoensso.carmine/wcar
                                               wcar-opts
                                               ((requiring-resolve 'taoensso.carmine/set) key value)))
                                            
                                            )}))

Shantanu Kumar 2026-02-22T15:28:09.728439Z

Just adding a tangential 2c here: If you need to do this in ClojureScript, anything touching requiring-resolve is very unlikely to work.

p-himik 2026-02-22T15:46:00.749699Z

@carsten.behring What do you mean "does not compile", what's the error?

2026-02-22T19:39:14.940019Z

It does work, I looked wrong. Thanks for solution !!