Eric Kolotyluk
Eric Kolotyluk

Reputation: 2243

How can I resolve Scala package name conflicts?

package net.kolotyluk

package object leaderboard {
  def randomLong: Long = {
    val random = scala.util.Random
    random.setSeed(System.nanoTime)
    random.nextLong
  }
}

Fails to compile with

[error] D:\Users\erick\repos\leaderboard\src\main\scala\net\kolotyluk\leaderboard\package.scala:7:24: object util is not a member of package net.kolotyluk.scala
[error]     val random = scala.util.Random
[error]                        ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed

I suspect the problem is I have package net.kolotyluk.scala.extras defined elsewhere. Why is it that Scala cannot find scala.util.Random? This seems like a defect in either the Scala compiler, or the Scala language itself.

Is there a way to import/specify an absolute package name instead of the relative package name which seems to be used here?

Using import __root__.scala.util.Random does not seem to work either as mentioned in https://docs.scala-lang.org/tour/packages-and-imports.html because

[error] D:\Users\erick\repos\leaderboard\src\main\scala\net\kolotyluk\leaderboard\package.scala:3:8: not found: object __root__
[error] import __root__.scala.util.Random
[error]        ^

Upvotes: 5

Views: 938

Answers (1)

Eric Kolotyluk
Eric Kolotyluk

Reputation: 2243

Doh! the documentation at https://docs.scala-lang.org/tour/packages-and-imports.html is wrong. It should be _root_ not __root__

Upvotes: 5

Related Questions