Dmitrii
Dmitrii

Reputation: 624

getting Value from scala enumeration

say i have enumeration like this

object SimpleEnum extends Enumeration{
 val ONE = Value(1)
 val TWO = Value(2)
 val THREE = Value(3)
 etc...
}

also i've a class which i want to be extending Ordered[SimpleEnum]

class SimpleClass(se: SimpleEnum) extends Ordered[SimpleEnum] {
  override def compare(that: SimpleEnum): Int = this.se.[here i want to get instance of SimpleEnum, just like i used to do in java, but unfortunately it's not possible]
}

So in SimpleClass i just need to get the Value attached to corresponding SimpleEnum val.

enter image description here

In java i'm able to declare a variable on enum and access an actual value corresponding to it by this var, in scala i'm looking for a way to get an instance of enum and reach out for its value.

Upvotes: 4

Views: 18835

Answers (2)

Pavlos Panteliadis
Pavlos Panteliadis

Reputation: 1555

The question is already answered but my approach is the following:

object SimpleEnum extends Enumeration{
 type SimpleEnum = Value
    val ONE = Value(1)
    val TWO = Value(2)
    val THREE = Value(3)
    etc...
}

class SimpleClass(val i : Int){
    def getNumber() {
        import SimpleEnum._
        SimpleEnum(i)
    }
}

Upvotes: -1

Łukasz
Łukasz

Reputation: 8663

It is possible, use id method.

scala> object E extends Enumeration { 
  val A = Value(1)
  val B = Value(7)
  val C = Value(2)
}
defined object E

scala> E.A.id
res7: Int = 1

scala> E.B.id
res8: Int = 7

scala> E.C.id
res9: Int = 2

Enumeration values can also be easly compared

scala> E.A < E.B
res10: Boolean = true

scala> E.C < E.B
res11: Boolean = true

scala> E.B < E.A
res12: Boolean = false

Refer to documentation for more

Edit

Your code in the picture is wrong. Firstly, as in your original code (that is not in the picture), SimpleEnum should be an object, not a class. As soon as you make that change your code won't compile and that should ring a bell.

You want SimpleClass to be able to wrap your enum values. Type of those values (i.e. ONE, TWO, THREE) is not SimpleEnum, it is SimpleEnum.Value. Objects of this type have id method.

class SimpleClass(se: SimpleEnum.Value) extends Ordered[SimpleEnum.Value] {
  override def compare(that: SimpleEnum.Value): Int = se.id
}

A common thing to do is to declare a type alias for Value with exact same name as the enum object. Then you can import this type and use it

object SimpleEnum extends Enumeration {
  type SimpleEnum = Value

  val ONE = Value(1)
  val TWO = Value(2)
  val THREE = Value(3)
}

import SimpleEnum.SimpleEnum

class SimpleClass(se: SimpleEnum) extends Ordered[SimpleEnum] {
  override def compare(that: SimpleEnum): Int = se.id
}

Note that Enumeration#Value already implements Ordered[Value]. You can verify it in the docs that I linked earlier.

There is no classic java enum in scala, but because the language is so cool, it was possible to create a regular class called Enumeration that with some tricks allows for similar behavior.

Upvotes: 5

Related Questions