Link vector element in Clojure

70 Views Asked by At

I'm trying to read a file in a macro in Clojure.

I'm launching my macro with that line :

(def result (rd [s (FileReader. (File. "myFile.txt"))] (.read s)))

where "rd" is the name of my macro.

The prototype of my macro is like that :

(defmacro rd
  ([] nil)
  ([arg] arg)
  ([[variable val] expr]
  )
)

The thing is that I can "execute" the FileReader, but when I'm trying to "execute" expr (.read s), it's not working because s is not known.

So I'm trying to link my elements of a vector to made s known, so I want "variable" pointed by val.

I'm not sure I'm in what I want to do, so if you see other ways, I'm up to it.

Thanks in advance guys.

1

There are 1 best solutions below

0
On BEST ANSWER

if you need to read the file at runtime, as you said, you need to introduce the var.. something like this:

(defmacro rd [[variable val] expr]
  `(let [~variable ~val]
     ~expr))

and then your macro call would expand to this:

(let [s (FileReader. (File. "myFile.txt"))] (.read s))