Search code examples
scalainheritanceparameter-passingimplicit

I have a function which takes an implicit parameter. How can I pass the parameter implicitly using implicit parameters of an instance of some class?


This is the function:

def execute[T, U, F[_]](t: T)(implicit
                                executor: Executor[F],
                                functor: Functor[F],
                                handler: Handler[T, U],
                                manifest: Manifest[U]): F[Response[U]] = {(do something)}

I have a class ElasticSearchRepositoryWrapper which inherits from somewhere (I couldn't find from where) handler variable. I have an instance of the class repo = ElasticSearchRepositoryWrapper(client, config, configName)
I want to do that:

class SomeService(val config: Config,
                             val configName: String = "products",
                             val client: ElasticClient)
                            (implicit val ec: ExecutionContext)
{
    repo = ElasticSearchRepositoryWrapper(client, config, configName)
    repo.client.execute {
      repo.delete(something)
    }
}

but it says, No implicits found for parameter handler: Handler[..., ...] for execute function
So, how can I pass that handler from repo to it?
Note: if class SomeService inherits from ElasticSearchRepositoryWrapper it finds it.


Solution

  • I am not sure what is the first parameter of execute. It looks like it wants some t, but you are passing in a block (not saying it is necessarily wrong, but kinda weird). But, assuming, that is indeed what you want, and repo.handler is accessible, you can pass it like this:

      repo.client.execute(repo.delete(something))(ec, implicitly, repo.handler, implicitly)