r/java • u/Decent-Decision-9028 • 3d ago
Would you rewrite a small Kotlin library in Java just to avoid stdlib ?
/r/Kotlin/comments/1w3f2fj/would_you_rewrite_a_small_kotlin_library_in_java/15
u/Joram2 3d ago
If it's an internal library that will be used by just you and coworkers, it may not matter.
If you want to make your library appeal to the broader public, then writing in pure Java without kotlin/scala support libs is important.
I'm actually facing a problem supporting a large enterprise project that uses three different third party libraries that use three different versions of the okhttp library which in turn uses three different versions of the kotlin stdlib, and this results in runtime clashes.
2
13
u/aboothe726 3d ago
In my opinion, applications can be written in whatever JVM language they want, but libraries belong in Java, or at least a JVM language without any additional dependencies. Internal libraries tightly coupled to the underlying applications are more flexible, since that's generally more about packaging/DRY and is still just a consistent reflection of the same existing architecture choices, but anything with broader applicability should be Java.
10
u/geodebug 3d ago
I think you got your answer but I’ll pile on that you should never write a library in anything but Java that you expect to be used with any number of JDK languages.
Too much bloat and unexpected incompatibilities. For example, if you’ve ever tried to use Spock (Groovy) for testing Kotlin code, you’ll soon learn that certain Kotlin structures won’t translate.
You’ll end up having to annotate your Kotlin code to make it more compatible.
1
u/johnnybgooderer 3d ago
It’s going to take annotations to write libraries in Java in a way that’s still ergonomic for kotlin applications that depend on it.
You have to choose a target and do your best to make it acceptable for other languages. Or just make it for your language and don’t even bother making it nice for the other ones until there is a demand.
7
u/morhp 3d ago
I would avoid Kotlin in the first place and I don't think just using core java is extra work.
Kotlin has some nice features, but very often they lead to worse code than what you'd do in Java in the first place. I for example think checked exceptions are pretty important to help you define a proper exception control flow in some cases (although I can see they're annoying with lambdas). Or the .? operator sometimes makes you go towards nullable values when some other design that avoids null completely might make handling of errors or missing data more clear.
Also Java in general is much better documented than Kotlin.
Also I think a lot of the Kotlin design choices like companion classes are relatively bad, I much prefer what the Java guys are prototyping at the moment with type classes/witness classes. Or records make more sense to me than Kotlin data classes. Data classes seem worse compatibility wise and they give you a lot of extra methods that you might not need or want.
5
u/johnnybgooderer 3d ago
Java really needs nullable and non nullable types. Nullaway just isn’t able to enforce a variety of common usecase. That’s what makes me reach for kotlin 99% of the time.
1
u/pjmlp 23h ago
I am programming since 1986, had a big focus on FP and LP languages during university, nullable types isn't that take or break feature in daily work.
Even less so, if the AI overlords are the ones doing the actual programming.
1
u/johnnybgooderer 23h ago
It’s a very very helpful feature that saves time and headaches. It lets you express more with less.
8
u/hadrabap 3d ago
I would never write it in Kotlin in the first place 🤣
Minimize dependencies as much as possible. When projects grow, they tend to manifest classpath hell.
6
u/Decent-Decision-9028 3d ago
Also, another point someone brought up was worrying about kotlin specific constructs leaking into the java facing api
-1
u/hadrabap 3d ago
Yep. I see it a lot. The contracts (API + model) written in fancy Kotlin (lots of Kotlin features) doesn't look native Java and makes things uglier than necessary 😀 I still recommend writing contracts using plain interfaces, actual Java Beans plus java.base as dependency (maybe Jakarta EE contracts, if it's intended for enterprise application). That's about it. Everything extra creates problems down the line in the life-cycle than good.
I really should kick myself in the butt and finalize the material/book I'm working on.
4
u/javaprof 3d ago
It's incorrect to compare Kotlin stdlib to Scala stdlib or even Guava. Kotlin stdlib much-much smaller and do not break ABI every release. So given that it's absolutely fine for me to use library that brings kotlin stdlib, especially because there are already a lot of libraries like that. What an library never ever should bring - is Guava
3
1
-1
u/koflerdavid 3d ago
It's not Kotlin and probably off-topic on that thread over there, but Kotlin should try to break up that standard library so people don't have to eat all of it when they want to use a Kotlin library.
-6
37
u/pgris 3d ago
In my java project I avoid kolin (and scala) libs, just to avoid dependencias with another stdlib. So I think you were right in doing so, keep the core in java.
BTW, Is it my imagination or back then when kotlin development started tha idea was to avoid having a stdlib at all, and reuse the java one?