Is Kotlin always dependent on Java? I mean it has ...
# random
m
Is Kotlin always dependent on Java? I mean it has a lot of wrappers around Java's libraries e.g. collections. Can it grow independently on the JVM?
n
Kotlin/JVM is dependent on the JVM, yeah
the real question IMO is whether Kotlin/JVM will always be limited by what's supported by JDK6
s
I believe there is already Kotlin that runs on more recent versions of the JDK. They mainly keep the Java 6 version for Android use
n
show me 🙂
I mean will they ever ship a feature that doesn't work on JDK6
No, but why would they?
n
be..cause...the JDK has added new features in the last 10 releases
s
and the features Kotlin adds can take advantage of the new byte code, but they're still going to be compatible with Java 6
s
Isn’t kotlin native trying to “expand” outside from JVM? And there is also kotlin serialization that avoids reflection so it can run on multiplatform.
g
What is "grow independently"? Kotlin uses big part of java stdlib, what is the point avoid it? It's a part of any JVM, not an additional runtime library The fact that Kotlin reuses big parts of java standard library makes interop so smooth as it is and one of the reasons of Kotlin success
s
@nanodeath they do
Copy code
Which versions of JVM does Kotlin target?
Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 6 compatible bytecode. If you want to make use of optimizations available in newer versions of Java, you can explicitly specify the target Java version from 8 to 13. Note that in this case the resulting bytecode might not run on lower versions.
https://kotlinlang.org/docs/reference/faq.html even in kotlin bytecode viewer you have a checkbox to see kotlin bytecode which targets java 8 compatible bytecode
m
Meaning...Kotlin always requires JDK, right? It cannot run without it, like Groovy does?
g
Whait what? Groovy also requires JDK (or more specifically JRE)
m
Yes 🙂 Groovy does not actually need JDK. Just the JRE. I meant to ask if that'll ever be the case with Kotlin too.
s
to run bytecode - jre to compile - jdk
kotlin compiles into valid java bytecode of specified target with default target of java 6 you use it the same way as any other program written in any other language that compiles into jvm bytecode - run using jre
m
To answer the initial question:
Is Kotlin always dependent on Java?
No, there are Kotlin/Native and Kotlin/JS. Neither require Java (at runtime) 😛 The compiler is written in Kotlin/JVM and hence requires Java for now in order to compile anything.
g
Kotlin perfectly run on jre, jdk is not needed on runtime, it's not different from Groovy or other JVM languages Also java standard library (which includes mentioned by you collections) is a part of JRE
☝️ 1
n
I don't really care about the bytecode part. Kotlin/JVM can compile to a variety of bytecode, but these are just optimizations. I'm also aware that kotlin-stdlib-jdk7 and kotlin-stdlib-jdk8 exist, but these are just additional glue for users to use, like converting Streams to Sequences and back. I'm just worried that there's useful stuff in the newer JREs (or I suppose Java Standard Libraries) that Kotlin will never be able to rely on; or can literally everything be "polyfilled" back into JDK6-compatible code? I'm skeptical.
m
What would be a good example?
n
what if I want to write some Kotlin that targets different versions of the JVM, but packaged in a single JAR
m
Is that possible with Java by now? 😯 Why do they have to be in a single JAR?
n
yes it is, Java 9 I think. simpler, just one artifact to distribute?
like if Kotlin targeted Java 9, we'd just have one
kotlin-stdlib
package that would automatically provide newer APIs for newer versions of Java
m
Distribution-wise Gradle hides all the complexity (until something goes wrong). How would that work? Would I have to annotate a function and say that it’s available on Java 12+ only for example?
s
And if Kotlin targeted Java 9, then it would be unusable on Android
n
I'm okay with that 😛
s
I don't think the Kotlin people are, as I would imagine Android is one of their biggest targets. When you can use Java 14 on a server, Kotlin does lose some of its luster
n
that's exactly my point -- when we're on Java 20 or whatever and Kotlin/JVM is still targeting Java 6...I just don't know what the plan is there. maybe when Kotlin 2.0 comes out it'll target whatever the new Android baseline is
or we'll see a Kotlin/JVM Kotlin/Android fork, though I highly doubt that
m
Personal opinion: In terms of usability Kotlin is far ahead of Java - whether it’s 8, 14 or whatever. Kotlin is slowly gaining its very own standard library and together with Multiplatform that is super powerful. Java isn’t anywhere near that versatility. In terms of language features Kotlin can evolve just as Java can. There doesn’t have to be a dependency. In terms of Java API you can interop with most Java libraries without issues. Java applications with Kotlin libraries isn’t too much different either. What’s the actual problem in using Kotlin with a newer Java version? Maybe I’m missing something here. And why the heck would anyone want to use Java over Kotlin unless they’re forced to, or need to maintain a large existing Java project? 😛
➕ 1
s
what are you talking about? kotlin is not targeting ONLY java 6 kotlin actually targets java 8 by default https://kotlinlang.org/docs/reference/using-gradle.html#attributes-specific-for-jvm
n
that changed in Kotlin 1.4. pleased to see it.
image.png
s
Yeah, I gotta agree with the previous person. I'm still not seeing the actual problem you have here
s
how does it relate to fact that you can use jdk 13 code from kotlin?
n
I don't see what's so hard to understand -- the JDK adds new features. if they can't be polyfilled back to JDK6, KotlinStdlib/KotlinCompiler can't use them.
s
no one is limiting to jdk6
and i don’t understand why you cannot understand that
no one is making wrappers for jdk 6
kotlin with target of jdk 13 will just use those jdk java classes
s
What's hard to understand is that there isn't a situation where that's happening
If you could provide an example where Kotlin targeting Java6 can't do something, that would go a long way toward illustrating the issue
n
kotlin with target of jdk 13 will just use those jdk java classes
no? JetBrains doesn't just magically write new classes that uses the latest features
s
what are you talking about
kotlin is a language that compiles into jvm bytecode
java is a language that compiles into jvm bytecode
if jvm bytecodes of 2 languages are interoperable - wow, you don’t need any generation
jetbrains don’t write any classes
they can make helping “wrappers” they don’t overwrite strings, they don’t overwrite arraylists, et cetera
g
Agre with Marc and Mykyta, it’s also not clear what exactly is not enough Kotlin, even if it target 1.6, can use any API additions of any new Java version, at least at this moment, most of what was added to Java 9-14 related or to Java language itself (like multiline strings or type inference), or to tooling (modules) or some new Java APIs (like new HttpClient) all of them are indifferent to Kotlin There are some bytecode optimisation in Java compiler, but if it really significant, it can be backported to Kotlin compiler, but I’m not aware about it There are big upcoming changes like Valhalla or Loom, which indeed would require language support for interop, but they are not ready yet, it’s not 100% sure how it will work, but that time Kotlin team probably introduce support/interop with those new JVM features to support them in Kotlin But for now there no problems with interop with Java 9-14 (which I’m aware of)
m
@sikri you’re mixing up Kotlin (the programming language) and Kotlin/JVM (Kotlin software that’s supposed to run on the JVM). Kotlin can also be used without the JVM, e.g. with JavaScript, WebAssembly and even as a native binary without any existing VM - it’ll provide its own. Since not every platform provides the all functionality of the Kotlin Standard Library that it can simply “wrap”, JetBrains provides their own implementations where needed. On top of that JetBrains is building more and more core libraries on their own. These libraries run on the JVM as well as on JavaScript and natively. They’re not dependent on the JVM but their implementation may be different depending on the platform and may delegate to platform-specific functionality under the hood. Examples are kotlinx-coroutines, kotlinx-io, kotlinx-datetime and kotlinx-serialization. I’m not sure what kind of “generation” you are referring to. Can you please elaborate?
➕ 1
s
@Marc Knaup topic starter was talking about jvm and jvm bytecode generation so I was talking only about jvm do jb make classes? yes, for sugar, for mpp, but they don’t make yet another kotlin.ArrayList for jvm, that’s what I am referring to. when they do something like that, they do it for kotlin-common and mpp
m
I agree, JetBrains doesn’t re-implement standard Java API unless necessary. So Kotlin/JVM is currently dependent on Java implementations. The initial question however is “Is Kotlin always dependent on Java?” and the answer clearly “no”, as there are Kotlin/JS and Kotlin/Native. Same for “dependent on JVM” and “dependent for JDK”. But that’s execution only. The compiler will need all three of them.