gpt4 book ai didi

scala - 如何在 Scala 中覆盖 `org.elasticsearch.client.FilterClient#doExecute()`?

转载 作者:行者123 更新时间:2023-12-04 18:57:40 25 4
gpt4 key购买 nike

特别是 "org.elasticsearch" % "elasticsearch" % "2.4.x" (它是 work 与更高版本
ElasticSearch 的位置 ActionRequest不再采用参数类型,但我们还不能更新到那些!)。

我们试图覆盖的 Java 方法定义如下( source ):

protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
in().execute(action, request, listener);
}

天真地试图覆盖它......
import org.elasticsearch.action._
import org.elasticsearch.client.{Client, FilterClient}

class DemoFilterClient(underlyingClient: Client) extends FilterClient(underlyingClient) {
override def doExecute[
Request <: ActionRequest[_],
Response <: ActionResponse,
RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]
](
action: Action[Request, Response, RequestBuilder],
request: Request,
listener: ActionListener[Response]
) = super.doExecute(action, request, listener)
}

... fails在我们尝试过的所有 Scala 版本中(2.11.11、2.11.12 和 2.12.4):
[info] Compiling 1 Scala source to /home/roberto/development/elasticsearch-scala-client-test/target/scala-2.11/classes ...
[error] /home/roberto/development/elasticsearch-scala-client-test/src/main/scala/com/gu/DemoFilterClient.scala:7:101: type arguments [Request,Response,RequestBuilder] do not conform to class ActionRequestBuilder's type parameter bounds [Request <: org.elasticsearch.action.ActionRequest[_ <: org.elasticsearch.action.ActionRequest[_ <: org.elasticsearch.action.ActionRequest[_ <: AnyRef]]],Response <: org.elasticsearch.action.ActionResponse,RequestBuilder <: org.elasticsearch.action.ActionRequestBuilder[Request,Response,RequestBuilder]]
[error] override def doExecute[Request <: ActionRequest[_], Response <: ActionResponse, RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]](action: Action[Request, Response, RequestBuilder], request: Request, listener: ActionListener[Response]) = super.doExecute(action, request, listener)
[error] ^
[error] one error found

更清楚地列出编译器错误:
type arguments
[Request,Response,RequestBuilder]
do not conform to class ActionRequestBuilder's type parameter bounds
[
Request <:
org.elasticsearch.action.ActionRequest[_ <:
org.elasticsearch.action.ActionRequest[_ <:
org.elasticsearch.action.ActionRequest[_ <: AnyRef]]],
Response <: org.elasticsearch.action.ActionResponse,
RequestBuilder <: org.elasticsearch.action.ActionRequestBuilder[Request,Response,RequestBuilder]
]

请注意 Request 上惊人的递归声明类型!

示例 sbt 项目可在 https://github.com/guardian/elasticsearch-scala-client-test 获得

最佳答案

我遇到过这样的情况,你之前有这种带有原始 Java 类型的 F 有界多态性,如果我没记错的话,这样的事情应该可以工作(至少它可以在你的示例项目中编译):

package com.gu

import org.elasticsearch.action._
import org.elasticsearch.client.{Client, FilterClient}

class DemoFilterClient(underlyingClient: Client)
extends FilterClient(underlyingClient) {

type AR[x <: ActionRequest[x]] = ActionRequest[x]

override def doExecute[
Request <: AR[_],
Response <: ActionResponse,
RequestBuilder <: ActionRequestBuilder[Request, Response, RequestBuilder]
](
action: Action[Request, Response, RequestBuilder],
request: Request,
listener: ActionListener[Response]
) = super.doExecute(action, request, listener)
}

似乎也应该可以使用 forSome 内联执行此操作。 ,但我刚才尝试过的迭代都没有奏效。

关于scala - 如何在 Scala 中覆盖 `org.elasticsearch.client.FilterClient#doExecute()`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com