3 ms·
Clojure libraries are good but you have Java and JS interop for mundane, everyday libraries. Is there anything wrong with interop? I think most Clojure librari
by dimitar 4y ago
Clojure libraries are good but you have Java and JS interop for mundane, everyday libraries. Is there anything wrong with interop?
I think most Clojure libraries rightfully focus on things that either only possible or needed in Clojure. Things like core async, typed clojure are not even possible to implement in other languages as libraries.
When thinking of examples: the cognitect/aws-api is amazing in it's ease and fun, yet AWS Java SDK is also easy to use from clojure. I prefer the former, but there isn't anything wrong with the latter.
- john2x 4y agoIf you're using interop for mundane, everyday things, why use Clojure then? I imagine you would end up writing non-idiomatic Clojure code to do the interop (e.g. working with mutable objects and not immutable datastructures).
- didibus 4y agoThe recommended design for Clojure programs is imperative shell, functional core. You use Clojure as your functional core, and it interops with mundane libraries for the imperative shell, such as database clients, http clients, logging, http server, graphics, sound, etc. It works beautifully, and the REPL helps with all of it.
- regularfry 4y agoA problem I have run into (admittedly, having not written as much clojure code as I'd like) is that some java libraries in particular can make interop distinctly unpleasant. Anything around the Spring framework, for instance, made me tear my hair out. Yes it's possible, but by the time you've compensated for the impedance mismatch you might as well have done a rewrite.
- didibus 4y agoHum, I've used Spring with Clojure a lot, never had issues. Things that are difficult to interop are java annotations based stuff, having to implement interfaces or subclass classes, and functional interfaces. But you can use Spring without annotations, which is my preferred way anyways, because I favor explicit over implicit.
- janetacarr 4y agoAs someone alluded to, Interop can be frustrating with big frameworks. Interop also relies heavily on reflection, and may require type hinting once it becomes a performance problem. Interop also implies the use of side-effects since we're relying on Java object. While a lot of Clojure's sequence and collection operations do work on Java sequences and Collections, it's not perfect and very easy to get bitten, requiring a Clojure.walk/postwalk and zipmap with coercion to get a good Java->Clojure container/sequence/collection. If we used interop for everything mundane, Clojure would really just be an S-expression shaped husk over Java code. Not a very great solution imho.
- didibus 4y agoI'll politely disagree. The mundane libraries I think you're talking about tend to be about IO, that's always going to be side-effectful in nature. Their APIs wouldn't be all that different even if implemented directly in Clojure. Type hinting isn't hard or time consuming, literally you can take 10min and have everything type hinted. Now I don't know about big frameworks, maybe I don't find a need for big frameworks? But even then, what kind of framework are you talking about? Using Spring MVC for example?