Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a result of ask to appropriate type?

Tags:

scala

akka

spray

I'm using ask (?) to get a value which is of type Set[String] from an Actor. However, the actor returns Future[Any].

What is the correct way to convert this Future[Any] to Future[Set[String]]?

val result : Future[Any] = myactor ? GetSomeValue
//convert Future[Any] to Future[Set[String]]
like image 683
Soumya Simanta Avatar asked Feb 13 '23 07:02

Soumya Simanta


1 Answers

There is method called mapTo available on Futures:

val result : Future[Set[String]] = (myactor ? GetSomeValue).mapTo[Set[String]]

It will raise an Exception if the cast is not successful. From the docs:

Creates a new Future[S] which is completed with this Future's result if that conforms to S's erased type or a ClassCastException otherwise.

like image 112
mavilein Avatar answered Feb 20 '23 18:02

mavilein