Reputation: 543
I'm trying to write a macro for use from ClojureScript to handle file I/O for a Reagent app. I get this error:
IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.Symbol
When I try to do the following:
(def file-string "/Users/luciano/Dropbox/projects/test/resources/blog/test.md")
(get-file file-string)
But I can do this just fine:
(get-file "/Users/luciano/Dropbox/projects/test/resources/blog/test.md")
This is the macro:
(defmacro get-file [fname]
(slurp (io/file fname)))
What am I doing wrong here?
Upvotes: 1
Views: 322
Reputation: 46
What this means is you are trying to call the macro with a symbol that doesn't have a value at runtime.
(def my-file "foo.txt")
(def file-str (get-file my-file))
Will work while
(defn foo [s]
(get-file s))
(foo "foo.txt")
will not.
Upvotes: 3
Reputation: 29958
You should not be using a macro for this.
A macro is best viewed as a compiler extension that is embedded in the source code.
All you need is a regular function, like:
(defn get-file
[fname]
(slurp (io/file fname)))
As far as doing I/O from a Reagent app, I'm not sure what you're goal is.
Upvotes: 0