Jallrich
Jallrich

Reputation: 469

Acces values inside a Future[Option[T]]

I have a class

case class SomeClass(name: String, number: Long)

And I save it in the database. When I want to recover it from the database I get is as

Future[Option[SomeClass]]

How can I access the parameters from inside this class?

Upvotes: 0

Views: 63

Answers (1)

machadoit
machadoit

Reputation: 136

Without any more context, and if you don't want to wait on the future, you can just map it:

responseFromDatabase.map(_.map(_.name)) 

to have Future[Option[String]] that may contain the name.

If you really want to get the value, you can also

Await.result(responseFromDatabase).map(_.name).get 

(or getOrElse with a default Name or something).

Can you provide more context?

Upvotes: 3

Related Questions