I'm having a problem accessing an object defined in the root package. If I have all my code in one file, it works fine, but when I split it across two files, I can't get it past the compiler.
This works fine:
All in one file called packages.scala
:
object Foo
val name = "Brian"
}
package somepackage {
object Test extends App {
println(Foo.name)
}
}
Witness:
$ scalac packages.scala
$ scala -cp . somepackage.Test
Brian
But if I split the code across two files:
packages.scala
object Foo {
val name = "Brian"
}
packages2.scala
package somepackage {
object Test extends App {
println(Foo.name)
}
}
it all fails:
$ scalac packages.scala packages2.scala
packages2.scala:3: error: not found: value Foo
So I try to make the reference to Foo
absolute:
...
println(_root_.Foo.name)
...
But that doesn't work either:
$ scalac packages.scala packages2.scala
packages2.scala:3: error: object Foo is not a member of package <root>
If Foo
is not a member of the root package, where on earth is it?
I think this is the relevant part in the spec:
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.
Source Scala Reference §9.2 Packages.
But don’t ask me why it works if you have the following in packages2.scala
:
object Dummy
package somepackage {
object Test extends App {
println(Foo.name)
}
}
Foo is a member of the root package, but you can't refer to it. It's a generic thing with JVM languages, (see How to access java-classes in the default-package? for Groovy, What's the syntax to import a class in a default package in Java?). It's the same for Scala.
From the Java answer:
You can't import classes from the default package. You should avoid using the default package except for very small example programs.
From the Java language specification:
It is a compile time error to import a type from the unnamed package.
The reason it works in one single file is because everything is available to the compiler at once, and the compiler copes with it. I suspect that this is to allow scripting.
Moral of the story: don't use the default package if you're not scripting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With