Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Christopher Brown edited this pageAug 30, 2017 ·14 revisions

Reader Conditionals allow youto write code that can target both Clojure and ClojureScript. This problem was previously solved bycljx which is now deprecated. There is aguide for Reader Conditionals atclojure.org.

Transitioning from cljx

After reading theReader 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 theclj andcljs files totarget others would mix them with other sources (i.e. using acommon folder).

Some examples of transitions arezelkova andbidi

General Considerations

  • Think about your new project structure: while some libraries can get away with fullcljc codebases, most application code will needclj,cljs, andcljc files. If one of your namespaces is mostly interop (e.g. date handling), it is preferable to have 2 distinctclj andcljs than 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
  • ns specs are one of the greatest differences betweenclj andcljs. See thewiki. For example, while:import is used in Clojure for user defined classes (defrecord anddeftype), it is only used in ClojureScript for Google Closure Classes.

Below examples do not match project structure above and are confusing. Some notes: Add example cljs file including a function from cljc. Add example in clj file including cljc.

For example, the followingclj code

(nsfoo.bar  (:require [baz.core:as baz])  (:import [baz.core BazRecord]))

should be

(nsfoo.bar  (:require [baz.core:as baz #?@(:cljs [:refer [BazRecord]])])  #?(:clj (:import [baz.core BazRecord])))

to be used incljc.

Seehttps://danielcompton.net/2016/05/04/requiring-records-clojure-clojurescript for fuller discussion and example.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp