首页 > 解决方案 > 我有一个带有隐式参数的函数。如何使用某个类的实例的隐式参数隐式传递参数?

问题描述

这是功能:

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)}

我有一个ElasticSearchRepositoryWrapper从某个地方(我无法从哪里找到)handler变量继承的类。我有一个repo = ElasticSearchRepositoryWrapper(client, config, configName)
我想这样做的类的实例:

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)
    }
}

但它说,No implicits found for parameter handler: Handler[..., ...]对于execute功能
那么,我怎样才能将它传递handlerrepo它呢?
注意:如果类SomeService继承自ElasticSearchRepositoryWrapper它找到它。

标签: scalainheritanceparameter-passingimplicit

解决方案


我不确定 的第一个参数是什么execute。看起来它想要一些t,但你正在传递一个块(不是说它一定是错误的,但有点奇怪)。但是,假设这确实是您想要的并且repo.handler可以访问,您可以像这样传递它:

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

推荐阅读