Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

future

That's really annoying when you write some highly concurrent code with Futures or Actors response and you have manually import ExecutionContext.Implicits.global. Tried to find some good explanation why it's not made as a default parameter, like it is done with Strategy in Scalaz Concurrent. That would be very helpful, not to insert/remove all those imports in the code. Or am i missing some logic?

like image 948
4lex1v Avatar asked Aug 20 '13 11:08

4lex1v


1 Answers

The general trend seems to be to require the user to explicitly import things like implicits, extra operators or DSLs. I think this is a good thing since it makes things less "magic" and more understandable.

But nothing stops you from defining a package-wide implicit value for your code. Note that if the implicit ExecutionContext was always imported by default, you wouldn't be able to do this.

In the package object:

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

In any class in the same package:

package myawsomeconcurrencylibrary

object Bla {
  future { ... } // implicit from package object is used unless you explicitly provide your own
}
like image 121
Rüdiger Klaehn Avatar answered Nov 10 '22 01:11

Rüdiger Klaehn