简体   繁体   English

.java和.scala类之间是否可能有循环依赖关系?

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

Lets say I have class A defined in .java file, and class B defined in .scala file. 可以说我在.java文件中定义了A类,在.scala文件中定义了B类。
class A use class B and class B use class A. A类使用B类,B类使用A类。
If I use java compiler I will have a compilation error because class B is not compiled yet. 如果我使用Java编译器,则由于B类尚未编译,将出现编译错误。 If I use scala compiler class A will not be found. 如果我使用Scala编译器,则不会找到类A。 Is there a compiler that can compile both together? 是否有可以同时编译的编译器?

I thought that Scala 2.7.2 introduced a joint compilation mode to do exactly this? 我以为Scala 2.7.2引入了联合编译模式来做到这一点?

Which version of scalac are you using, and is it running with this mode disabled? 您正在使用哪个版本的scalac ,并且在禁用此模式的情况下可以运行它?

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导致找不到Class A时-您是否意识到以后仍需要使用javac编译纯Java文件? Scalac's joint compilation mode doesn't actually produce *.class output for the Java files, merely compiles the Scala classes against their signatures. Scalac的联合编译模式实际上不会为Java文件生成*.class输出,而只是根据其签名编译Scala类。 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. 因此,尽管现在已经编译了Scala类,但是您仍然需要在以后编译Java文件,尽管这对于javac不是问题。

Create fake class B in Java. 在Java中创建伪造的B类。 Declare there only members which are referenced from A. Methods' bodies can be empty. 仅声明从A引用的成员。方法的主体可以为空。 Compile A.java and B.java together. 一起编译A.java和B.java。 Then throw away B.class and compile B.scala. 然后扔掉B.class并编译B.scala。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM