22

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?

Martin McNulty
  • 2,601
  • 3
  • 22
  • 26

3 Answers3

28

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)
  }
}
Debilski
  • 66,976
  • 12
  • 110
  • 133
  • Thanks - I wonder if defining the Dummy object puts an implicit "package {" at the start of the file, meaning it's in scope later on? – Martin McNulty Mar 22 '12 at 12:51
  • Looks like it does indeed: `scalac -Xprint:typer packages2WithDummy.scala` → `package { … ` – Debilski Mar 22 '12 at 13:47
21

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.

Community
  • 1
  • 1
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
0

Ran into this when trying to import the main App entrypoint into a test. This may be an evil hack, but putting package scala at the top of the entrypoint definition seems to have made the object globally available. This may be evil, but it works.

E.g. /src/main/scala/EntryPoint.scala

package scala

object EntryPoint extends App {
  val s = "Foo"
}

/src/test/scala/integration/EntryPointSuite.scala

package integration 
import org.scalatest.FlatSpec

class EntryPointSuite extends FlatSpec {
  "EntryPoint" should "have property s" in {
    val ep = EntryPoint.main()
    assert(ep.s == "Foo")
  }
}
Logister
  • 1,852
  • 23
  • 26