-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add experimental inline instrumented defn macro #702
Conversation
thanks for trying it out (and checking for the meta)! - I will refactor that to support it. I think the |
Sure, please let me know if I can help in any way. I tried some combinations of |
src/malli/experimental.cljc
Outdated
schema (as-> (map ->schema parglists) $ (if single (first $) (into [:function] $)))] | ||
`(def ~name (m/-instrument {:schema ~schema :report ~report} | ||
(c/fn ~name | ||
~@(map (fn [{:keys [arglist prepost body]}] `(~arglist ~prepost ~@body)) parglists) | ||
~@(when-not single (some->> arities val :meta vector)))))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a way to keep the meta with minimum effort, if you want to use it:
schema (as-> (map ->schema parglists) $ (if single (first $) (into [:function] $)))] | |
`(def ~name (m/-instrument {:schema ~schema :report ~report} | |
(c/fn ~name | |
~@(map (fn [{:keys [arglist prepost body]}] `(~arglist ~prepost ~@body)) parglists) | |
~@(when-not single (some->> arities val :meta vector)))))))) | |
schema (as-> (map ->schema parglists) $ (if single (first $) (into [:function] $))) | |
meta-to-defn (cond-> meta doc (assoc :doc doc))] | |
`(do | |
(c/defn ~name ~meta-to-defn ~(map :arglist parglists)) | |
(set! ~name | |
(m/-instrument {:schema ~schema :report ~report} | |
(c/fn ~name | |
~@(map (fn [{:keys [arglist prepost body]}] `(~arglist ~prepost ~@body)) parglists) | |
~@(when-not single (some->> arities val :meta vector))))))))) | |
But this code does not work with Clojure (because you can't set!
root bindings). I'm supposing ->defn
is considered an implementation detail, so we can send an additional parameter (like is-clojurescript?
) to handle the Clojure case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this! based on that I went with a strategy to produce an actual defn that just delegates to the instrumented version.
3b2943d
to
4c2661a
Compare
cribbed from metosin#702 Co-authored-by: Daniel Vingo dan@danvingo.com
cribbed from metosin#702 Co-authored-by: Daniel Vingo <dan@danvingo.com>
This was implemented in #825 |
This came out of the discussion over here:
#695
When working in cljs having the code output by macros in the same namespace you're working in will enable repl-driven development without needing to think about refreshing the namespace where
instrument!
was called.This PR adds a macro, inspired by Guardrails/Ghostwheel
>defn
macro, which will output an instrumented function.A parallel
noop
ns can be added to allow controlling this via shadow-cljs config (see https://github.com/fulcrologic/guardrails#dead-code-elimination)I needed to add the
{:registry (m/default-schemas)}
lines because the schemas used for the parsing code will not always be available if the user is using a custom registry.