Sambit
Sambit

Reputation: 8011

Compilation issue with enum in Scala using type with value

I am new to Scala, I have the following code. I am trying to run it in Eclipse. It shows compilation issue.

object TestEnum2 extends Enumeration {

  type Donut = Value
  val Glazed      = Value("Glazed")
  val Strawberry  = Value("Strawberry")
  val Plain       = Value("Plain")
  val Vanilla     = Value("Vanilla")

  def show() {
    println(s"Vanilla Donut string value = ${Donut.Vanilla}");
  }

  def main(args: Array[String]) {
    show();
  }
}

It shows the following error as given in the image. It gives error

Not Found: value Donut

enter image description here

Upvotes: 0

Views: 460

Answers (1)

Thilo
Thilo

Reputation: 262534

Donut is the name of type. The values are called TestEnum2.Vanilla or in your case, since it is already in scope, just Vanilla.

println(s"Vanilla Donut string value = ${Vanilla}");

As an aside, enumerations in Scala 2.x are a bit rough. I recommend either taking a look at enumeratum or using Java enums.

Upvotes: 2

Related Questions