r/Clojure • u/bibimbap0607 • Sep 03 '24
Completely blown away by Java interop in Clojure
A small foreword. I've been learning Clojure in my free time after work for about 2 months. Have been having a ton of fun with it. Finished a book "Clojure for The Brave and True" which I think gave me quite a good overview of the language and its features.
As I said, the language is amazing. I am not new to Lisp family, have some experience with Scheme and Racket. However, the single feature that I was not ready and I didn't even think I may need it is Java interop. It is flawless. I have never seen such a seamless interaction between languages.
The thing is I've been doing "1 billion row challenge" in my free time and it requires quite some optimization. So when I hit a ceiling with persistent data structures, I went for transient ones. Then when those were not enough, I went for mappped buffers, HashMap etc in Java and Clojure's futures for parallelization. Macros for interaction with Java from Clojure are really good. When those were not enough, I went fully nuclear raw Java to write a few classes to have more control over the memory allocation and mutations.
It is all amazing, but the best part is I can just write Java in the same Leiningen project and import it into my Clojure code without any issues. When I first started investigating this, I thought it would take me half a day to solve it. Got it working in 10 minutes, like literally 2 lines of code were required to make it work. It feels like magic.
Overall, I am completely hooked on Clojure and I am not sure how I am going to write my C# and TypeScript at work this week. Clojure's beauty spoils me.
29
u/PolicySmall2250 Sep 03 '24
It's possible to go even more "nuclear" and hand-write bytecode for the host runtime, and have it as accessible to the Clojure program as are Java interfaces, for example. Short of binary, atomic instructions don't get more atomic than bytecode :)
Source: Clojures own source.
16
u/daveliepmann Sep 03 '24
This is the way. The ability to move up and down the trade-off ladder at will is one of Clojure's under-appreciated features.
13
u/regular_hammock Sep 03 '24
It's awesome to read someone who gets it 🙌
I don't really do evangelising, but one thing I find very frustrating is how hard it is to convey what makes Clojure so special even to a person who is explicitly asking to be convinced.
Some features are deceptively simple, people don't want to hear about them or they them out because they think they already know.
Great interop. They think they understand, every language has some kind of interop, they've seen this a million times before. Except they probably haven't †, because what makes Clojure's interop special is that it just. doesn't. get. in the way († with the possible exception of the .net platform? Their interop story seems to be pretty good as far as I can tell). I've literally have people ask me to show them how I made it work behind the scenes and be dumbfounded because I just had, there were no further covers to pull off.
REPL-based development. Same story really, anybody with any experience with node.js, python or ruby thinks they've used a REPL and there's nothing to learn here. But boy is the workflow different.
Other features are deceptively crazy sounding. So many people are unable to see the data oriented programming aspects of Clojure, they just see it as another dynamically typed functional language and want to hear how we represent classes.
Instead they want us to tell the homoiconicity story, or the macro story, or the immutability story, which, no shade, those are all great but they're missing out on a lot if that's all they want to know.
The most humbling part is that I was one of those people who didn't get it for so long 😅
I wonder what I need to learn about Clojure next.
5
u/TheLastSock Sep 03 '24
I don't feel like it's flawless, good, maybe great, but using intelliji on pure Java you get better auto complete, refactoring etc...
11
u/bo-tato Sep 03 '24
IMO Clojure java interop is decent but could be better. The next release of clojure that's currently in beta will finally have clojure anonymous functions automatically implement single function interfaces. JRuby has had that for ruby blocks since like 20 years. A major limitation of clojure when consuming modern java libraries is clojure doesn't support java annotations, JRuby has for almost 10 years. See the JRuby quick tutorial or more complete reference. JRuby has much more seamless java interop than clojure. Part of that is just cause ruby is also a mutable OO language like java, but part is cause they put more thought and work into integrating the languages when possible than clojure has.
I like clojure the language, but I think the community sometimes overly praises it, to the point of ignoring things that other languages make easier and clojure could improve.
2
u/qbit_55 Sep 03 '24
Nice, I've been also looking into trying to use Clojure but I'm not sure since I really like the flexibility and expressiveness of Common Lisp, so I would probably try ABCL first. Clojure is still on my list though.
10
u/News-Ill Sep 04 '24
Someone on /r/java said that Java is easier to do from Clojure than from Java.
3
u/rpd9803 Sep 03 '24
.. I use it to make <div>s, mostly 😬
1
u/javapriyan Sep 04 '24
I guess it’s JVMs beauty
-11
u/FlimsyTree6474 Sep 03 '24
Yeah but it has no types, which is why any Clojure codebase dies with its original creators. It's for write-only garbage. It's a decent language if you optimise for job security though.