How to determine Clojure runtime?

I wrote a very simple date library in Clojure, and I would like to be able to use it when my code runs in the JVM and when it is compiled into Javascript with ClojureScript. A fly in a stroke is how to determine the runtime environment in which the code resides so that the macro can use the string syntax analysis mechanism. Each platform date object

How to write a library that can be reused on different deployment platforms such as JVM, JS, CLR, etc.? I know that you can spoof and parse date strings with a regular expression, but it will not easily cover the case of parsing month names from a wide variety of human languages ​​that are well versed in date parsing libraries.

+4
source share
3 answers

Use the solution to find out which vars are defined:

user=> (resolve '*clojurescript-version*)
nil
user=> (resolve '*clojure-version*)
#'clojure.core/*clojure-version*

user=> *clojure-version*
{:major 1, :minor 5, :incremental 1, :qualifier nil}
+3
source

You can use these different characters:

http://clojuredocs.org/clojure_core/clojure.core/clojure-version

Returns clojure version as a printable string.

http://clojuredocs.org/clojure_core/ clojure.core / clojure-version

The version info for Clojure core, as a map containing :major :minor 
:incremental and :qualifier keys. Feature releases may increment 
:minor and/or :major, bugfix releases will increment :incremental. 
Possible values of :qualifier include "GA", "SNAPSHOT", "RC-x" "BETA-x"
+1
source

Clojure has a special conditional reader rule for switching I / O load times.

In your example, it would be something like this:

(def ^:dynamic *runtime*
  #?(:clj :clojure)
  #?(:cljs :clojurescript))

(print *runtime*) ;; :clojure
0
source

All Articles