Using Reader Conditionals, compile to Clojure, and ClojureScript
Codes in Clojure and ClojureScript are almost the same but there are few differences. For instance, libraries provided by Java and JavaScript are quite different. Host environments between JVM and JavaScript runtimes are also different.
Before the version 1.7, we use the cljx
plugin to keep compatibility between them. However, it requires a tooling and the style is not clean.
For such a situation, Clojure 1.7 introduced Reader Conditionals are introduced. Reader Conditionals have richer semantics than cljx
, such as default expressions and form splicing and no prepossessing.
Using Reader Conditionals, we can share source code among Clojure, ClojureScript, and ClojureCLR. In this recipe, we will demonstrate how we can share single source files between Clojure and ClojureScript.
Getting ready
We will use the Figwheel template to generate a Clojure and ClojureScript project:
$ lein new figwheel cljc-example -- --om
The generated...