Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“Convert” Option[x] to x in Scala

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.

like image 212
mahoosh Avatar asked Nov 04 '13 10:11

mahoosh


People also ask

What is option () in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

What is getOrElse in Scala?

As we know getOrElse method is the member function of Option class in scala. This method is used to return an optional value. This option can contain two objects first is Some and another one is None in scala. Some class represent some value and None is represent a not defined value.


1 Answers

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

like image 104
Dao Lam Avatar answered Sep 28 '22 20:09

Dao Lam