如何在 Scala 中覆盖 `org.elasticsearch.client.FilterClient#doExecute()`?
How to override `org.elasticsearch.client.FilterClient#doExecute()` in Scala?
特别是 "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
获得
我已经 运行 遇到过这种 F 有界多态性与原始 Java 类型之前的情况,如果我没记错的话,这样的事情应该有效(至少它在您的示例项目中编译):
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
内联执行此操作,但我刚才尝试的 none 迭代已经奏效了。
特别是 "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
获得我已经 运行 遇到过这种 F 有界多态性与原始 Java 类型之前的情况,如果我没记错的话,这样的事情应该有效(至少它在您的示例项目中编译):
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
内联执行此操作,但我刚才尝试的 none 迭代已经奏效了。