Class is not visible unless I declare it in a package

ぐ巨炮叔叔 提交于 2019-12-13 18:12:41

问题


I have a class AppComponents in AppLoader.scala in projectDir/app

My tests are in projectDir/test/ControllerSpec/UserControllerSpec.scala

In UserControllerSpec.scala, I tried to create an instance of AppComponents but the compiler couldn't find the AppComponents class

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

But if I include statement package app in Apploader.scala then the compiler is able to find AppComponents and the above code compiles.

I don't understand this behavior.


回答1:


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.

So AppComponents should only be visible to other classes/traits/etc. outside a package. Because

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

it looks like UserControllerSpec does declare a package and can't see the empty package's members.

This behavior is probably for consistency with Java, see answers to Import package with no name Java.



来源:https://stackoverflow.com/questions/50739913/class-is-not-visible-unless-i-declare-it-in-a-package

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