Hello here, I was reading about the mixed kotlin/j...
# getting-started
g
Hello here, I was reading about the mixed kotlin/java support and was wondering. In the docs it advises to run Kotlin compiler first (i use maven) and later java compiler. I tried to create a kotlin class that depends on java class and it worked. My question is why? Java compilation happens after. If it would be the other way around (java class depending on kotlin class and running java compiler without compiling kotlin first) it wouldnt work. So why does it work when you compile kotlin classes that depend on java classes that are not compiled (yet)? I also tried running the kotlin compilation alone and in target folder I saw only compiled kotlin class but not the java class it depends upon. A quick look over compiler documentation didnt provide me with an answer. So maybe someone here knows or could point me in the right direction. Thanks in advance.
m
I believe that that Kotlin compiler will internally compile Java classes that it detects Kotlin classes depend on. I remember a talk from several years ago, that was saying if you take a Java code base and add a single Kotlin file, the compile time doubles since all the Java classes get compiled twice.
e
the Kotlin compiler doesn't compile Java, but it does read Java source and class files to make them available to the Kotlin code it does compile
g
@mkrussel i wonder what happens if you have circular dependencies. Kotlin depends on java that depends on kotlin
@ephemient i dont think there are any class files at that point as java classes have not been compiled yet
s/java classes/java files
m
Compiling was probably the wrong term. Parsing would be better. The circular dependencies would be handled similarly to how it is handled within just Kotlin and Java.
g
@mkrussel right. If its just parsing, then java's dependencies dont have to be satisfied. Very interesting, thank you. Regarding the doubling compile time, would it be more accurate to say that time increases only proportional to the number of actual dependencies of kotlin files rather than whole codebase. Anyway, very interesting thoughts, thanks.
e
Kotlin compiler reads class files and JARs containing class files from dependencies, like a Java compiler does. it just has the additional feature of also reading Java source files to build a model of what the Java compiled output would look like
g
@ephemient i understand that its what it does. But my situation is when you dont have classes at all. Clean project with 2 files: one kotlin, one java (.java). You run kotlin compiler it compiles kotlin class just fine. Java file is not compiled.
I guess @mkrussel's answer sheds some light on it
Would be nice to know the exact details of that parsing, whether it can be controlled and which versions of java classes it supports
Do you know maybe a channel here that i could join where such question would fit?
e
#compiler possibly
my understanding is that it's based off of IntelliJ's Java model, whether it's reading class files or source files
g
@ephemient basically what you say is the same as what @mkrussel says. That it reads, but not compiles java sources kotlin files depend upon and that is enough for him to compile kotlin files. Thanks now I get it. Should have read your responses more carefully. Thanks.