.java 和 .scala 类之间是否可能存在循环依赖?
假设我在 .java 文件中定义了类 A,在 .scala 文件中定义了类 B。
A 类使用 B 类,B 类使用 A 类。
如果我使用 java 编译器,则会出现编译错误,因为 B 类尚未编译。如果我使用scala编译器A类将找不到。有没有可以同时编译两者的编译器?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为Scala 2.7.2引入了 联合编译模式 到底能做到这一点吗?
您使用的是哪个版本的 scalac?它是否在禁用此模式的情况下运行?
编辑:等一下,当你说 scalac 导致 A 类找不到时 - 你是否意识到你之后仍然需要使用
javac
编译纯 Java 文件? Scalac 的联合编译模式实际上并不为 Java 文件生成*.class
输出,而只是根据 Scala 类的签名来编译它们。因此,您之后仍然需要编译 Java 文件,不过既然 Scala 类已经编译好了,这对于javac
来说应该不是问题。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 forjavac
now that the Scala classes have been compiled.在 Java 中创建假类 B。只声明从 A 引用的成员。方法体可以为空。
将A.java和B.java一起编译。然后扔掉B.class,编译B.scala。
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.