4 ms·
How much does clojure take advantage of its hosted status? Can anyone who builds clojure software for multiple runtimes comment? It would obviously be extremel
by jfaucett 11y ago
How much does clojure take advantage of its hosted status?
Can anyone who builds clojure software for multiple runtimes comment? It would obviously be extremely nice if you could program libraries once and have them run on the JVM, CLR, and inside the Browser. In practicality how difficult/easy is it to build cross-runtime libraries and how much reuse can you get out of clojure libs b/c of its hosted status?
- 147 11y agoThere's cljc which is a conditional reader that lets you code in clj and cljs in the same file. It's not that difficult to write a library that runs on both. But there are differences in the runtimes, of course. Some examples: https://github.com/stuartsierra/component https://github.com/stuartsierra/component https://github.com/plumatic/schema https://github.com/plumatic/schema (uses .cljx, the precursor to .cljc)
- jfaucett 11y agoNice examples. Am I correct in guessing that this code: (ns com.stuartsierra.component (:require [com.stuartsierra.dependency :as dep] [com.stuartsierra.component.platform :as platform])) requires the correct platform.clj or platform.cljs depending on the runtime? And that this: #?(:clj (defmethod clojure.core/print-method SystemMap [system ^java.io.Writer writer] (.write writer "#<SystemMap>")) :cljs (extend-protocol IPrintWithWriter SystemMap (-pr-writer [this writer opts] (-write writer "#<SystemMap>")))) is how you perform different operations in a single file based on runtime?
- 147 11y agoThe first part requires platform on both platforms. You are correct about the latter.
- jffry 11y agoI can't speak to running Clojure outside the JVM, but on the JVM there's good interop with native Java, which allows using Java-based tools and libraries. This instantly gives you access to a whole ecosystem of stuff written for Java. It's very easy to use the Java AWS SDK directly from Clojure code, for example.
- diomedestydeus 11y agoI was working on a clojure/clojurescript app a while ago. I was hoping to share code between the two. This was pre-clj 1.7 with reader conditionals, so I did some manual footwork to try and resolve the small differences. However the biggest issue I repeatedly ran into was libraries. One of clj's biggest strengths (and why it has an awesome library ecosystem) is that you can just nab a battle tested java lib and write a tiny clj wrapper around it. Those libraries are, obviously, not going to run in js land. As clj matures and we see more pure clj libraries the situation will hopefully improve, but I felt somewhat thwarted by my attempt.
- arohner 11y ago> In practicality how difficult/easy is it to build cross-runtime libraries and how much reuse can you get out of clojure libs b/c of its hosted status? It depends on what you're trying to do, and whether the platforms help. Doing something 'pure' like math or datastructures: trivial. Writing a common interface to two different host libraries (e.g. DateTime in JVM & Browser): manageable. Doing something 'host-y', like graphics, with no host libraries in common: very hard. Basically, Clojure doesn't get in the way of cross-platform concerns, but it might still be a difficult problem.
- harlanji 11y agoTo add, it's not great at the moment and will be more confusing until Clojure/ClojureScript 1.7.x is the minimum supported due to the introduction of reader conditionals (cljc), as they're not backwards compatible. Cljx is the old method that cljc should obviate, but there'll be a transition period. I work for a SaaS/PaaS company using Clojure internally for 2 years and was looking into making a universal client (jvm, clr, js, ios, android)... I'd surely have used a Cljx core with Lein profiles for each platform prior but now it's up in the air, may still have to go that way if we're supporting older Clojure. Each approach on its own is easy, but now there are two and the unofficial one (cljx) is more widely supported than the official one (cljc).