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

放肆的年华 提交于 2019-12-07 05:04:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!