0

How can we search for a string in an enum of strings?

object FilterByDimensions extends Enumeration {
  type FilterByDimensions = String
  val Instance = "Instance"
  val Platform = "Platform"
  val Region = "Region"

  def isSupported(s: String) = 
  FilterByDimensions.values.exists(_.toString.equalsIgnoreCase(s))
}

This method is not working. Tried this also.

def isSupported(s: String) = 
  FilterByDimensions.values.exists(_.equalsIgnoreCase(s))
Nithin Chandy
  • 686
  • 1
  • 10
  • 28
  • That's way not how you use Enumeration, if you insist on using it. http://www.scala-lang.org/api/current/scala/Enumeration.html – som-snytt May 17 '17 at 01:26
  • There are many alternatives for Enumerations. I wrote this article about a bunch of them, with the advantages and disadvantages, maybe it helps you getting a general idea: http://pedrorijo.com/blog/scala-enums/ – pedrorijo91 May 17 '17 at 19:00
  • (and the follow up: http://pedrorijo.com/blog/scala-enums-part2/) – pedrorijo91 May 17 '17 at 19:00

2 Answers2

1

If I understand your question that you want to check if a string exists in FilterByDimension enum, the your FilterByDimension should be as below

object FilterByDimensions extends Enumeration {
  type FilterByDimensions = String
  val Instance = Value("Instance")
  val Platform = Value("Platform")
  val Region = Value("Region")

  import scala.util.control.Breaks._
  def isSupported(s: String) = {
    var exists = false
    breakable {
      for(value <- FilterByDimensions.values){
      exists = s.equalsIgnoreCase(FilterByDimensions(value.id).toString)
      if(exists){
        break
      }
    }
    }
    exists
  }
}

Edited
for pattern matching, its better to use case class

case class FilterByDimensions(value: String)

object FilterByDimensions {
  object Instance extends FilterByDimensions("Instance")
  object Platform extends FilterByDimensions("Platform")
  object Region extends FilterByDimensions("Region")

  val values = Seq(Instance, Platform, Region)
}

And you can call it as below

val ins = "Instance"
    ins match {
      case FilterByDimensions.Instance.value => println("instance match")
      case FilterByDimensions.Instance.value => println("progressing")
      case FilterByDimensions.Instance.value => println("region match")
      case _ => println("doesn't match")
    }
Ramesh Maharjan
  • 41,071
  • 6
  • 69
  • 97
1

Maybe you're looking for:

scala> object X extends Enumeration { val Y = Value }
defined object X

scala> def f(s: String) = util.Try(X.withName(s)) match { case util.Success(X.Y) => "ok" case _ => "nope" }
f: (s: String)String

scala> f("Y")
res0: String = ok

scala> f("Z")
res1: String = nope

The two little features people request is matching by name and look-up by name which doesn't throw.

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • I'm actually trying to use the values in the enum for pattern matching. filter match { case FilterByDimensions.Instance // do something case FilterByDimensions.Platform // do something else } – Nithin Chandy May 17 '17 at 03:15
  • You need to show complete code. What is filter? See my snippet showing matching on an enum value. – som-snytt May 17 '17 at 03:19
  • Actually, it worked. Thanks a lot. Just one question. Is this the standard way of matching string enums? – Nithin Chandy May 17 '17 at 03:32
  • This is how to do it with Enumeration, but folks prefer `case object` extending a `sealed trait`, and there are other strategies. http://stackoverflow.com/questions/1898932/case-objects-vs-enumerations-in-scala – som-snytt May 17 '17 at 04:27