Why is the global ExecutionContext not the default parameter in a future block?

This is really annoying when you write some highly competitive codes with Futures or Actors response, and you manually import ExecutionContext.Implicits.global . I tried to find a good explanation of why it was not made as the default parameter, for example done with Strategy in Scalaz Concurrent . This would be very useful not to insert / delete all this import data in the code. Or am I lacking in logic?

+6
source share
1 answer

The general trend seems to require the user to explicitly import things like implicits, additional operators, or DSL. I think this is good, because it makes things less β€œmagical” and more understandable.

But nothing prevents you from defining the implicit value of the package for your code. Please note: if the implicit ExecutionContext has always been imported by default, you cannot do this.

In the package object:

 package object myawsomeconcurrencylibrary { implicit def defaultExecutionContext = scala.concurrent.ExecutionContext.global } 

In any class in one package:

 package myawsomeconcurrencylibrary object Bla { future { ... } // implicit from package object is used unless you explicitly provide your own } 
+10
source

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


All Articles