6

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?

4

1 回答 1

10

总体趋势似乎是要求用户显式导入隐式、额外运算符或 DSL 等内容。我认为这是一件好事,因为它使事情变得不那么“神奇”并且更容易理解。

但是没有什么能阻止你为你的代码定义一个包范围的隐式值。请注意,如果默认情况下始终导入隐式 ExecutionContext,您将无法执行此操作。

在包对象中:

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

在同一包中的任何类中:

package myawsomeconcurrencylibrary

object Bla {
  future { ... } // implicit from package object is used unless you explicitly provide your own
}
于 2013-08-20T14:36:08.477 回答