Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala: can't import object from root scope

I Have the following code: (src/main/scala/coins/coins.scala)

object Main extends App { 

  def countChange(money: Int, coins: List[Int]): Int = {
  [...]

And I'm trying to reference it from a test like this: (src/test/scala/coins/CoinsSuite.scala)

package coins

import org.scalatest.FunSuite

class CoinsSuite extends FunSuite {

  import Main.countChange
  test("only onw way to pay $0") {
  [...]

And I get the following error:

not found: value Main [error] import Main.countChange

But on the other hand, from an sbt console it works fine

If I declare any package in the main file, like

package x

object Main extends App { 
  Console.println("Hello World!")

Then I can correcly issue import x.Main.countChange

Is there limitation on root package or on singleton objects visibility that I'm not aware of?

-- added

just to complete the answer, a couple of useful links at SO

https://stackoverflow.com/a/2030159/47633

https://stackoverflow.com/a/9822212/47633

https://stackoverflow.com/a/9822227/47633

like image 340
opensas Avatar asked Oct 06 '12 18:10

opensas


1 Answers

Java (and Scala according to the same convention) is grumpy about importing things in the unnamed package, which is not the same thing as the root package. Put Main into a package.

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

like image 85
Rex Kerr Avatar answered Nov 02 '22 13:11

Rex Kerr