问题
Lets say I have class A defined in .java file, and class B defined in .scala file.
class A use class B and class B use class A.
If I use java compiler I will have a compilation error because class B is not compiled yet. If I use scala compiler class A will not be found. Is there a compiler that can compile both together?
回答1:
I thought that Scala 2.7.2 introduced a joint compilation mode to do exactly this?
Which version of scalac
are you using, and is it running with this mode disabled?
Edit: Wait a second, when you say scalac leads to Class A not being found - did you realise that you still need to compile the pure Java files with javac
afterwards? Scalac's joint compilation mode doesn't actually produce *.class
output for the Java files, merely compiles the Scala classes against their signatures. Hence you still need to compile the Java files afterwards, though this shouldn't be a problem for javac
now that the Scala classes have been compiled.
回答2:
Create fake class B in Java. Declare there only members which are referenced from A. Methods' bodies can be empty. Compile A.java and B.java together. Then throw away B.class and compile B.scala.
来源:https://stackoverflow.com/questions/4464059/is-it-possible-to-have-a-circular-dependency-between-java-and-scala-classes