-
Notifications
You must be signed in to change notification settings - Fork 792
Using cljc
Sebastian Bensusan edited this page Aug 9, 2015
·
14 revisions
Reader Conditionals allow you to write code that can target both Clojure and ClojureScript. A more comprehensive introduction can be found here. This problem was previously solved by cljx which is now deprecated.
After reading the Reader Conditionals Design Page you'll notice the conversion is straightforward:
(try
(dangerous!)
(catch #+clj Exception
#+cljs :default
e
(solve-it)))is transformed to:
(try
(dangerous!)
(catch #?(:clj Exception
:cljs :default)
e
(solve-it)))Some things to have in mind while making the transition:
- Make sure you delete all the files generated by cljx before starting with the transition. While some projects would generate the
cljandcljsfiles totargetothers would mix them with other sources (i.e. using acommonfolder).
Some examples of transitions are zelkova and bidi
- Think about your new project structure: while some libraries can get away with full
cljccodebases, most application code will needclj,cljs, andcljcfiles. If one of your namespaces is mostly interop (i.e. date handling), it is preferable to have 2 distinctcljandcljsthat one file full of reader conditionals. A possible project structure:
src
|-- clj
| |-- feed
| |-- api.clj
| |-- db.clj
|-- cljs
| |-- feed
| |-- components.cljs
| |-- store.cljs
|-- cljc
| |-- feed
| |-- util.cljc
| |-- schema.cljc
-
nsspecs are one of the greatest differences betweencljandcljs. See the wiki. For example, while:importis used in Clojure for user defined classes (defrecordanddeftype), it is only used in ClojureScript for Google Closure Classes.
For example, the following clj code
(ns foo.bar
(:import [baz.core BazRecord])
(:require [baz.core :as baz]))should be
(ns foo.bar
(:require [baz.core :as baz :refer BazRecord]))to be used in cljc.
- Rationale
- Quick Start
- Differences from Clojure
- [Usage of Google Closure](Google Closure)