我有一堂打字课
trait ResponseHandler[+A, -B[_]] {
def handle[C](response: WSResponse, parser: A => B[C]): HTTPCallResult[C]
}
及其示例
type Identity[X] = X
implicit object JsonResponseHandler extends ResponseHandler[JsValue, Identity] {
def handle[C](response: WSResponse, parser: JsValue => C): HTTPCallResult[C] = {
// implementation
}
}
在调用函数时
post[A, In, Out[_]](parser: In => Out[A])(implicit handler: ResponseHandler[In, Out]) = {
// implementation
}
就像这样
post { resp: WSResponse =>
resp.body == "alive"
}
它抛出编译时错误
[error] no type parameters for method post: (parser: In => Out[A])(implicit handler: ResponseHandler[In,Out]) exist so that it can be applied to arguments (WSResponse => Boolean)
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : WSResponse => Boolean
[error] required: ?In => ?Out[?A]
[error] .post { resp: WSResponse =>
[error] ^
[error] type mismatch;
[error] found : play.api.libs.ws.WSResponse => Boolean
[error] required: In => Out[A]
[error] .post { resp: WSResponse =>
[error] ^
[error] ambiguous implicit values:
[error] both object WSResponseHandler in object ResponseHandler of type
[error] ResponseHandler.WSResponseHandler.type
[error] and object WSResponseWithErrorsHandler in object ResponseHandler of type ResponseHandler.WSResponseWithErrorsHandler.type
[error] match expected type ResponseHandler[Any,Out]
[error] .post { resp: WSResponse =>
[error] ^
[error] three errors found
然而,当像这样调用时
import ResponseHandler.Identity
post { resp: WSResponse =>
(resp.body == "alive").asInstanceOf[Identity[Boolean]]
}
它起作用了。有没有可能以某种方式帮助编译器推断类型?我试过def getIdtentity[A](a: A): Identity[A] = a.asInstanceOf[Identity[A]]
,但不起作用。
1条答案
按热度按时间yr9zkbsy1#
尝试
这应该有助于推断类型。
隐含的顺序为
handler
、ev
。其他选项: