← Back to context

Comment by Jeaye

2 days ago

> Are there Clojure libraries that don't use JVM(/JS/...)-specific stuff that works on any Clojure platform/dialect? Can such libraries be used on Jank out of the box?

Correct. Any Clojure code which doesn't use interop will generally work with Clojure, ClojureScript, Clojure CLR, jank, etc. There are some exceptions, where different dialects don't fully implement a Clojure feature, but this is generally the case.

> Or do library authors have to do something explicit in their libraries to enable their use in specific platforms/dialects?

Clojure also supports reader macros to enable forms for specific dialects. This is basically like an #ifdef in the C world, where library devs can check if the code is currently being compiled for Clojure, ClojureScript, jank, and so on. This allows you to have a public function, for example, which internally just uses a reader conditional to do the correct thing. For example:

    (defn sleep [ms]
      #?(:clj (Thread/sleep ms)
         :jank (let [s (/ ms 1000)
                     ns (* (mod ms 1000) 1000000)
                     t (cpp/timespec. (cpp/long. s) (cpp/long. ns))]
                 (cpp/nanosleep (cpp/& t) cpp/nullptr))))

That's using the currently working C and C++ interop to call the POSIX C function. The same could be done for the C++ version. This function can now be used in both Clojure and jank with no difference to the consumer.