o1ctav
o1ctav

Reputation: 121

Scala enum not seen in another object

I have the following enum in MyEnum.scala:

object MyEnum extends Enumeration {
  val A, B, C, D, E = Value
}

In another object, I am trying to assign an enum to a variable:

object SomeOtherObject {
  def main(args: Array[String]): Unit = {
      val myValue = MyEnum.A
  }
}

However, I am getting:

 error: not found: value MyEnum

Both of these object are defined in the default package.

Any idea what is causing this?

Upvotes: 1

Views: 130

Answers (1)

pedrorijo91
pedrorijo91

Reputation: 7845

If I define everything in the same scala file everything works fine

$ cat x.scala
object MyEnum extends Enumeration {
  val A, B, C, D, E = Value
}

object SomeOtherObject {
  def main(args: Array[String]): Unit = {
      val myValue = MyEnum.A
  }
}

$ scalac x.scala
# no output but produces the .class files

If you are defining in 2 different files, try to add a package to the files, and possibly import.

I also tried to define the enum in a y.scala file, and the main in the z.scala file. It also compiles:

$ scalac y.scala z.scala

Upvotes: 1

Related Questions