1

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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That is the reason! Thank you
This answer is not correct! get-file is a macro. A macro manipulates literals (and returns ClojureScript forms to use in place of the macro). This get-file macro, invoked on a string literal, can use the string and thus read the indicated file (on the build machine, at compile time); but if invoked on a symbol, then the symbol is what it receives. The compiler doesn't process a macro's arguments before handing them to the macro. If you really want to go this way, check out the resolve function for what the macro might do with the symbol.
0

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.

1 Comment

I think the goal is to pull in some resource statically at compile time and embed the data in the resulting cljs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.