Hello, can you explain this macro code?
(defmacro inspect-caller-locals []
(->> (keys &env)
(map (fn [k] [`'~k k]))
(into {})))
(inspect-caller-locals)
;;=>{}
(let [a 1 b 2] (inspect-caller-locals))
;;=> {a 1, b 2}
What do you need explained about it? &env
is the environment the macro is evaluated in.
That's a variant of the local-map
macro is the library that my company released a while back https://github.com/worldsingles/commons/blob/master/src/ws/clojure/extensions.clj#L152
^ @zengxh
consider local a and 1. in [`'~k k], if first element is 'a how does the second get value 1?
That's how macros work. Maybe this is a better question for #beginners ? I'm not sure where to start...
(I don't mean to be rude but I want to make sure we're started from the appropriate place)
No problem. 🙂
ah ok. captures.
Yeah, &env
is magic but it's the captured environment by definition.
There's &form
as well for the source form of the macro. Per https://clojure.org/reference/macros#_special_variables
thank you
I'm working on wrapping netty's http2 and I got stuck with this bit: https://netty.io/4.1/xref/io/netty/example/http2/helloworld/server/HelloWorldHttp2HandlerBuilder.html
I was trying to use proxy to do the same thing (with my own reified handler of course), but I can't seem to get build
to work properly.
(proxy [AbstractHttp2ConnectionHandlerBuilder] []
(build
; ([] (proxy-super build))
([decoder encoder initial-settings]
(let [handler (http2-handler decoder encoder initial-settings)]
(proxy-super frameListener handler)
handler))))
If I have the zero-arity version then it fails with java.lang.UnsupportedOperationException: build
.
Without the zero-arity version it fails with clojure.lang.ArityException: Wrong number of args (1) passed to: my.http2/fn--391/fn--392
(which is, I figure, the 3-arity build
defined).
I don't know if it's because the build
s in the abstract superclass are protected or that if I enter the "super zone" with proxy-super from zero-arity it can't find Clojure's 3-arity one?
Is there any way I can express this in Clojure or do I have to write raw Java for it?