Methods prepared and the result of Future in Scala cannot be called directly

I noticed that the ready and result of scala.concurrent.Future cannot be called directly. When I call them in the REPL, I get an error. So I have to call Await.ready and Await.result .

This is good, but it looks a bit uncomfortable. Now I wonder:

  • why they cannot be called directly;
  • why in this case they are displayed as an open API.
+6
source share
1 answer

The reasons lie in the white paper :

A future trait implements a recognized trait using the ready () and result () methods. These methods cannot be called directly by clients - they can only be called by the execution context.

Thus, library developers force you to use these methods in an appropriate environment (execution context , possibly a third-party , or through an awaitable object to explicitly indicate locking operations).

+4
source

Source: https://habr.com/ru/post/945282/


All Articles