I have following abstract class:
abstract class FieldProvider[+T: Writes](db: DB)(implicit i: RequestAction, j: ExecutionContext) {}
and following implementations:
class LengthProvider extends FieldProvider ...
object LengthProvider extends ((DB) => LengthProvider) {
def apply(v1: DB): LengthProvider = new LengthProvider(v1)
}
class WidthProvider extends FieldProvider ...
object WidthProvider extends ((DB) => WidthProvider) {
def apply(v1: DB): WidthProvider = new WidthProvider(v1)
}
The reason why I have these apply
methods is because I need following configuration map:
val providers: Map[String, ((DB) => FieldProvider)] = Map(
"length" -> LengthProvider,
"width" -> WidthProvider
)
So that I can initialize the providers by the string containing their name:
providers("length")(db) // returns a new instance of LengthProvider
Now, my problem is that all these providers constructors require two implicit variables. But I don't know how to include it into the function definition (DB) => FieldProvider
. So, essentially, the apply
method signature should be something like (DB)(implicit RequestAction, ExecutionContext) => FieldProvider
, but I don't know if there is a correct syntax for what I'm trying to do.
I could also give up and pass them explicitly:
object WidthProvider extends ((DB, RequestAction, ExecutionContext) => WidthProvider) {
def apply(v1: DB, v2: RequestAction, v3: ExecutionContext): WidthProvider = new WidthProvider(v1)(v2,v3)
}
But then I'll have to pass them explicitly elsewhere, instead of providers("length")(db)
, I'd have to write providers("length")(db, implicitly[RequestAction], implicitly[ExecutionContext])
, which doesn't feel right.