I am not able to create a Future as explained here. It says you can create a Future directly using the following code:
import akka.dispatch.Await
import akka.dispatch.Future
import akka.util.duration._
val future = Future {
"Hello" + "World"
}
val result = Await.result(future, 1 second)
Using the exact same code, I get an errormessage, saying: error: could not find implicit value for parameter executor: akka.dispatch.ExecutionContext. All I can find about the ExecutionContext is, that you can “do stuff” with it. In the documentation, the only line I found was:
implicit val ec = ExecutionContect.fromExecutionService(yourExecutionServiceGoesHere)
But this was not usefull to me. Has anyone any suggestion on that topic for me? How can I create a new Future without asking an Actor?
If you have an
ActorSystemthen it will have anExecutionContextwhich can be used here. It must beimplicit, unless you pass it explicitly to theFuture.applymethod.For the purposes of demonstration, if you just want to see how this works in the REPL:
(There is also an implicit conversion
ActorSystem => ExecutionContexton theExecutionContextobject.)To create modular code, without creating a context just before the point of use, consider making the context an abstract member of a trait: