Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 804
Martin McNulty Avatar asked Mar 22 '12 12:03

Martin McNulty


2 Answers

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)
  }
}
like image 55
Debilski Avatar answered Sep 29 '22 14:09

Debilski


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.

like image 30
Matthew Farwell Avatar answered Sep 29 '22 15:09

Matthew Farwell