Class 不可见,除非我在包中声明它

Class is not visible unless I declare it in a package

我有一个 class AppComponentsAppLoader.scalaprojectDir/app

我的测试在 projectDir/test/ControllerSpec/UserControllerSpec.scala

UserControllerSpec.scala 中,我试图创建一个 AppComponents 的实例,但编译器找不到 AppComponents class

override def components: BuiltInComponents = new AppComponents(context) //doesn't compile

但是如果我在 Apploader.scala 中包含语句 package app 那么编译器就能够找到 AppComponents 并且上面的代码可以编译。

我不明白这种行为。

Top-level definitions outside a packaging are assumed to be injected into a special empty package. That package cannot be named and therefore cannot be imported. However, members of the empty package are visible to each other without qualification.

所以 AppComponents 应该只对其他 classes/traits/etc 可见。在包裹外面。因为

if I include statement package app in Apploader.scala then the compiler is able to find AppComponents

看起来 UserControllerSpec 确实声明了一个包,但看不到空包的成员。

此行为可能是为了与 Java 保持一致,请参阅 的答案。