4
votes

I've written a very simple date library in Clojure and I'd like to be able to use it both when my code runs in a JVM and when it is compiled to Javascript with ClojureScript. The fly in the ointment is how to detect which runtime environment the code is in so that a macro might use the platform's own date string parsing mechanism. Each platform's date objects

How should one write a library that may be reused across different deployment platforms like the JVM, JS, CLR etc.? I know that one might cheat and parse date strings with a regex but that will not easily cover the case of parsing month names from the large variety of human languages which the built-in date parsing libraries solve quite nicely.

3
Take a look at cljx - github.com/lynaghk/cljx - edbond

3 Answers

2
votes

Use resolve to find out which vars 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}
0
votes

You can use these misc vars:

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"
0
votes

Clojure has special Conditional reader rule to switch b/w runtimes.

In your example, it would something like:

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

(print *runtime*) ;; :clojure