views:

193

answers:

5

I often find myself with an Option[T] for some type T and wish to test the value of the option against some value. For example:

val opt = Some("oxbow")
if (opt.isDefined && opt.get == "lakes") 
   //do something

The following code is equivalent and removes the requirement to test the existence of the value of the option

if (opt.map(_ == "lakes").getOrElse(false))
 //do something

However this seems less readable to me. Other possibilities are:

if (opt.filter(_ == "lakes").isDefined)

if (opt.find(_ == "lakes").isDefined) //uses implicit conversion to Iterable

But I don't think these clearly express the intent either which would be better as:

if (opt.isDefinedAnd(_ == "lakes"))

Has anyone got a better way of doing this test?

+13  A: 

How about

if (opt == Some("lakes"))

This expresses the intent clearly and is straight forward.

Walter Chang
Duh! Sometimes I can be extremely stupid
oxbow_lakes
Well, it happens to me all the time ;-)
Walter Chang
Sometimes the simplest solution is the best :)
Ula Krukar
A: 

You can use for-comprehension as well:

for {val v <- opt if v == "lakes"}
  // do smth with v
Alexander Azarov
A: 

I think pattern matching could also be used. That way you extract the interesting value directly:

val opt = Some("oxbow")
opt match {
  case Some(value) => println(value) //Doing something
}
This does not test against the value - only that it is present. The reason I want to avoid a `match` is that this consequently results in the `else` logic residing in multiple places (i.e. the `case None` and if the value is not the desired one)
oxbow_lakes
+4  A: 

Walter Chang FTW, but here's another awkward alternative:

Some(2) exists (_ == 2)
Daniel
Daniel, that is *awful*! `:-)`
oxbow_lakes
HRJ
This is actually the standard way as far as I'm concerned.
extempore
It's a slight pity that Option doesn't implicitly convert to `Seq`, which would allow for `Some(1) contains 1`.
retronym
@retronym Yeah, but, on the other hand, `Some(2).toSeq contains 2` works. Too round-about for my tastes.
Daniel
Perhaps contains should be added to Option directly.
retronym
@retronym Not a bad idea, actually. Go to http://lampsvn.epfl.ch/trac/scala and open an enhancement ticket if you truly care about it.
Daniel
+2  A: 
val opt = Some("oxbow")
opt match {
  case Some("lakes") => //Doing something
  case _ => //If it doesn't match
}
Ken Bloom