A new feature in the upcoming Clojure 1.7 release is Reader Conditionals. Reader Conditionals let you write portable Clojure/ClojureScript code in the same file, and wrap platform specific code in an expression which will only be executed on the platform it’s meant for.
A simple example is
(try (>! c msg) (catch #?(:clj Exception :cljs js/Object) e) .... ))
One important restriction on Reader Conditionals is that they can only be used in Clojure files with a new
I was recently porting some code to use Reader Conditionals and got this error message when compiling the namespace with a Reader Conditional in the file:
CompilerException java.lang.RuntimeException: Conditional read not allowed, compiling: <filename.clj>
I was very puzzled by this. After thinking for a little bit I started deleting parts of the file, to see if there was something in it which was stopping Reader Conditional use. I got down to a bare namespace with just a single reader conditional in the whole file and it still wouldn’t compile. I looked at my
project.clj file to see if there was a weird setting in there causing problems but that looked fine too.
Eventually I realised that this file didn’t have a
.cljc extension. I’d converted some of the files when I started, but then started adding Reader Conditionals to one I hadn’t converted. After changing the extension from
.cljc everything compiled fine and I was on my way again.
I opened CLJ-1734 to give more descriptive error messages for Reader Conditional failures. Feel free to vote for it if you think it would be a helpful feature.