How do I convert the query result to the appropriate type?

I am using ask

( ?

) to get a value that 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]]

      

+3


source to share


1 answer


Futures has a method called mapTo

:

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

      



This will throw an exception if the cast is not successful. From the docs :

Creates a new Future [S] that terminates with this future result if it matches an erasable type S or a ClassCastException.

+7


source







All Articles