Why is my object not a member of package <root> if it's in a separate source file?

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.

Tags:

Packages

Scala