Is it more idiomatic Scala to pass in ExecutionContext
per method like
class Foo {
def bar(a: Int, b: Int)(implicit ec: ExecutionContext): Future[Int] = {
Future(a + b)
}
def baz(a: Int, b: Int)(implicit ec: ExecutionContext): Future[Int] = {
Future(a - b)
}
}
or better to pass in ExecutionContext
per class like
class Foo(implicit ec: ExecutionContext) {
def bar(a: Int, b: Int): Future[Int] = {
Future(a + b)
}
def baz(a: Int, b: Int): Future[Int] = {
Future(a - b)
}
}
Is one style usually more preferred in Scala world because it causes less surprises, is easier to read, or for other reasons? Please give some references if possible.
The two options have different semantics so neither is idiomatic.
The first option allows the caller to specify the execution context at call time and allows different contexts to be used for different calls.
The second option requires that the same context is used for all calls.
The choice depends on the semantics that you want for your class and methods.