简体   繁体   中英

How to access an Enum object inside a scala class

I'd like to know what the proper syntax is for accessing an enum inside a class, from both inside and outside it.

My code:

final class Celig {

  object eNums extends Enumeration {
    type eNums = Value
    val NAM, YEE = Value
  }

  // Here I'd like to assign a method that always returns a specific enum value
  var nammy = eNums.NAM
  def getNam: eNums.eNums = nammy
}

This code compiles, but it's on the ugly side.

I would like to know if a value of the Enum can be accessed in a cleaner way from inside the class, for example without the "eNums.eNums", and I'm also curious as to how to access a value of the enum from outside the class.

Edit: I would also like to know if it is possible to use a value in the default constructor that takes a value of the eNums type, for example, adding (car: Celig.eNums ) to the class header.

Thanks

Does something like the following do what you're looking for?

final class Celig {
  object ENums extends Enumeration {
    val NAM, YEE = Value
  }

  def getNam: ENums.Value = ENums.NAM
}

I would caution that Enumeration is generally not used very much in the world of Scala. Often we use sealed trait s or sealed abstract class es instead to simulate algebraic datatypes which subsume Enumeration s.

In particular I would've probably rewritten your code something like the following:

sealed trait ENums
case object NAM extends ENums
case object YEE extends ENums

final class Celig {
  def getNam: ENums = NAM
}

This way of doing things has nice compiler support in the form of exhaustiveness checking for match statements and also allows for richer hierarchies than what Enumeration offers.

sealed trait ENums
case object NAM extends ENums
case object YEE extends ENums

final class Celig {
  def switchOnEnums(x: ENums): Int = x match {
    case NAM => 1
  }
}

results in

[info] Set current project to Test 1 (in build file:/scalastuff/)
[info] Compiling 1 Scala source to /scalastuff/target/scala-2.11/classes...
[warn] /scalastuff/src/main/scala/Main.scala:35: match may not be exhaustive.
[warn] It would fail on the following input: YEE
[warn]   def switchOnEnums(x: ENums): Int = x match {
[warn]                                      ^
[warn] one warning found
[success] Total time: 5 s, completed Sep 16, 2016 1:13:35 AM

And for a more complex hierarchy that still enjoys the compiler assisted exhaustiveness checking from before

sealed trait Day
case object Monday extends Day
case object Tuesday extends Day
// Got tired of writing days...

sealed trait Month
case object January extends Month
case object February extends Month
// Got tired of writing months...

sealed trait Date
case class CalendarFormat(day: Day, month: Month, year: Int) extends Date
case class UnixTime(sinceUTC: BigInt) extends Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM