Is it possible to have a circular dependency between the .java and .scala classes?

Suppose I have a class A defined in a .java file and a class B defined in a .scala file.
class A use class B and class B use class A.
If I use the java compiler, I will have a compilation error because class B has not yet been compiled. If I use scala, a class A compiler will not be found. Is there a compiler that can compile both together?

+4
source share
2 answers

I thought Scala 2.7.2 introduced a collaborative compilation mode to do just that?

What version of scalac are you using, and does this mode work in disconnected mode?

Edit: Wait a second when you say that scalac causes class A not to be found - did you realize that you still need to compile clean Java files using javac ? Scalac's collaborative compilation mode does not actually produce *.class output for Java files, but simply compiles the Scala classes against signing them. Therefore, you still need to compile Java files, although this should not be a problem for javac now that the Scala classes have been compiled.

+18
source

Create a fake class B in Java. Declare that only members referenced by A. Methods bodies can be empty. Compile A.java and B.java together. Then throw away B.class and compile B.scala.

+1
source

Source: https://habr.com/ru/post/1332388/


All Articles