Metadata Reader Macros and fnks
atroche opened this issue · comments
Hey guys,
I'd expect fnks to keep the metadata I give them via reader macros, but they don't:
=> (keys (meta ^{:has-meta? true} (fn [] true)))
(:has-meta?)
=> (keys (meta ^{:has-meta? true} (fnk [] true)))
(:schema)
Is this intentional? I came across this while using fnks for my reagent components. Reagent uses function metadata to help work out e.g. the key or display name of its components.
In a similar vein, it looks like fnks don't have a “name” property like normal Clojure functions do:
my.namespace=> (.-name (fn blah [] 1))
"my$namespace$blah"
my.namespace=> (.-name (fnk blah [] 1))
nil
Not intentional, we just forgot to transfer the meta -- thanks for the report. Looks like Schema does it correctly, so it's just a matter of fixing the fnk
wrappers in plumbing. PR welcome!
As for the .-name
, I wasn't aware ClojureScript had that (or is that Clojure 1.8?), but again a fix is welcome.
I made an attempt based on how schema does it, but the metadata still isn't being retained. Any tips?
Thanks! Just took a look, a couple comments:
- defnk already puts the meta on the var, which is where it typically goes:
user> (defn ^:foo foo [])
#'user/foo
user> (meta foo)
nil
user> (meta #'foo)
{:ns #<Namespace user>, :name foo, :file "/private/var/folders/_g/_nhxhpvn249c7ylfjwftblvr0000gp/T/form-init1510179789589322624.clj", :column 1, :line 1, :foo true, :arglists ([])}
i.e., even normal defn doesn't put meta on the fn, and I don't think you want to do anything different for defnk since it also imposes a performance hit.
- For
fnk
, the meta should go on the function as you suggest. Your code does work, but if you look atfnk-form
there's an if statement, and you only covered the "else" case. To make it cover all cases, you also need to add the metadata inpositional-fnk-form
. (keep in mind, there is one other caller outside this file that would need to be fixed as well). - In
fnk-form
, you could just usewith-meta
rather thanvary-meta
, since there won't be any metadata on the form already (and this will be more performant).
Thanks again for your help. Hope this is enough to get you to a working patch, if not let me know if you have questions, or I can also take a crack when I get a chance. Cheers!
Fixed by #120. Thanks for your help!