“Convert” Option[x] to x in Scala

mahoosh picture mahoosh · Nov 4, 2013 · Viewed 40k times · Source

I working with play for scala (2.1) and I need to convert an "Option[Long]" value to "Long".

I know how to do the opposite, I mean:

  def toOption[Long](value: Long): Option[Long] = if (value == null) None else Some(value)

But in my case, I have to pass a value of "Option[Long]" as a type into a method that takes "Long". Any help please.

Answer

Dao Lam picture Dao Lam · May 19, 2014

If you have x as Option[Long], x.get will give you Long.