4lex1v
4lex1v

Reputation: 21567

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

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?

Upvotes: 6

Views: 1392

Answers (1)

Rüdiger Klaehn
Rüdiger Klaehn

Reputation: 12565

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
}

Upvotes: 10

Related Questions