.java 和 .scala 类之间是否可能存在循环依赖?

发布于 2024-10-07 20:10:30 字数 148 浏览 0 评论 0原文

假设我在 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把昨日还给我 2024-10-14 20:10:30

我认为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 for javac now that the Scala classes have been compiled.

尽揽少女心 2024-10-14 20:10:30

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文