Question

Can literal tags capture local context bindings, so for instance if i have a tag #my/closuretag whose reader function is closuretag

(defrecord MyRecord [f])
(defn closuretag [f] (MyRecord. (eval f)))

For above defintions, #my/closuretag inc would work fine but (let [foo 1] #my/closuretag #(inc foo)) would fail.

Can a literal tag be made to work for this case? May be by a macro instead of a plain reader function?

Edit:

Here is something weird, If i change closuretag function to (defn closuretag [f] f), I get a proper closure with scoped variables bound inside the function which is callable! Why does using record here causing f to be a list of symbols?!

Was it helpful?

Solution

I am guessing that we should infer from literal tag vs. tagged element and the behavior described in your edit, that you are referring to readers installed in data_readers.clj, and not readers for data through (edn/) read or read-string.

Since these are reader functions, they are going to behave like macros in that you can conceptually consider the arguments to be quoted and results evaluated as code.

So, you want this

(defrecord MyRecord [f])

(defn my-tag-reader [f] (list ->MyRecord f))

To get the behavior of this

(def my-record (let [foo 1] #my/example-tag #(inc foo)))

((:f my-record)) ;=> 2

When my-tag-reader is the (namespaced qualified) reader function of #my/example-tag in the data_readers.clj map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top