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

假如想象 提交于 2019-12-17 22:52:25

问题


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?


回答1:


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)
  }
}



回答2:


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.




回答3:


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")
  }
}


来源:https://stackoverflow.com/questions/9822008/why-is-my-object-not-a-member-of-package-root-if-its-in-a-separate-source-fil

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