> I really like the &env. It has saved a lot of tedious work a couple of
> times .. but I have only found use for the keys of the map that gets
> passed like in the following example.
> I don't understand what the val part of the map contains? I have
> failed at attempts to figuring it out.. Can somebody tell me what the
> val part contains and how one could use it?
It contains a LocalBindings object which the compiler uses internally to
keep track of a local binding. Note that unlike the keys, the values of
&env are not a stable API, they're implementation details and may well
change. When he added &env I think Rich said he'd look at giving the
values of &env a proper API as part of the future Clojure in Clojure
compiler.
But you can poke at them like this:
(defmacro foo []
(def lb (first (vals &env))))
(let [a (+ 1 3)]
(foo))
(.sym lb)
;; a
(.idx lb)
;; 1
(.name lb)
;; "a"
(.isArg lb)
;; false
(.canBeCleared lb)
;; true
(.init lb)
;; #<StaticMethodExpr
;; clojure.lang.Compiler$StaticMethodExpr@1a9d267d>
(.args (.init lb))
;; [#<ConstantExpr clojure.lang.Compiler$ConstantExpr@51d2bb9f>
;; #<ConstantExpr clojure.lang.Compiler$ConstantExpr@621bedb0>]
If you're using Emacs + SLIME, the SLIME inspector is quite useful for
exploring unknown values. After evaling the first two forms above, put
the cursor over "lb" and press C-c I (note that's shift + i). You'll
get something like this:
clojure.lang.Compiler$LocalBinding@25de152f
--------------------
Type: class clojure.lang.Compiler$LocalBinding
Value: clojure.lang.Compiler$LocalBinding@25de152f
---
Fields:
sym: a
tag:
init: clojure.lang.Compiler$StaticMethodExpr@1a9d267d
idx: 1
name: a
isArg: false
clearPathRoot: clojure.lang.Compiler$PathNode@1740d415
canBeCleared: true
You can then press enter on any of the field values or class names to
get more detail about them.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en