如何从另一个调用 return 的函数?
How call function which is return from another one?
我正在尝试调用函数来编组 HttpResponse from spray. unmarshal
function described here. response is HttpResponse
考虑 3 种代码变体:
第一个
val func = unmarshal[MyType]
func(response)
第二
unmarshal[MyType].apply(response)
第三
unmarshal[MyType](response)
为什么我的第三个代码变体无法编译,而前两个代码变体可以编译?编译器 returns:
[error] found : spray.http.HttpResponse
[error] required: spray.httpx.unmarshalling.FromResponseUnmarshaller[MyType]
[error] (which expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpResponse,MyType]
[error] unmarshal[MyType](response)
有没有一种方法可以比创建临时变量或直接调用 apply
方法更优雅地调用 unmarshal
返回的函数?
该函数的签名是(来自您的link):
def unmarshal[T: FromResponseUnmarshaller]
所以 T
需要隐含的证据来证明它有这样一个 FromResponseUnmarshaller
。签名实际上编译成类似这样的东西:
def unmarshal[T](implicit evidence: FromResponseUnmarshaller[T]): HttpResponse ⇒ T
这意味着,unmarshal
函数实际上还采用了一个隐式参数,该参数应该将您的 MyType
转换为 HttpResponse
。
在你的第一个例子中,你调用了 val func = unmarshal[MyType]
这使得编译器为你插入隐含的。在您的第三个示例中,
unmarshal[MyType](response)
response
正在占据隐式参数的位置,它应该是 FromResponseUnmarshaller
,而不是 HttpResponse
。
这样的调用需要是:
unmarshal[MyType](fromResponseUnmarshsaller)(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
This is the original method call Here you apply response to the returned function.
我正在尝试调用函数来编组 HttpResponse from spray. unmarshal
function described here. response is HttpResponse
考虑 3 种代码变体:
第一个
val func = unmarshal[MyType]
func(response)
第二
unmarshal[MyType].apply(response)
第三
unmarshal[MyType](response)
为什么我的第三个代码变体无法编译,而前两个代码变体可以编译?编译器 returns:
[error] found : spray.http.HttpResponse
[error] required: spray.httpx.unmarshalling.FromResponseUnmarshaller[MyType]
[error] (which expands to) spray.httpx.unmarshalling.Deserializer[spray.http.HttpResponse,MyType]
[error] unmarshal[MyType](response)
有没有一种方法可以比创建临时变量或直接调用 apply
方法更优雅地调用 unmarshal
返回的函数?
该函数的签名是(来自您的link):
def unmarshal[T: FromResponseUnmarshaller]
所以 T
需要隐含的证据来证明它有这样一个 FromResponseUnmarshaller
。签名实际上编译成类似这样的东西:
def unmarshal[T](implicit evidence: FromResponseUnmarshaller[T]): HttpResponse ⇒ T
这意味着,unmarshal
函数实际上还采用了一个隐式参数,该参数应该将您的 MyType
转换为 HttpResponse
。
在你的第一个例子中,你调用了 val func = unmarshal[MyType]
这使得编译器为你插入隐含的。在您的第三个示例中,
unmarshal[MyType](response)
response
正在占据隐式参数的位置,它应该是 FromResponseUnmarshaller
,而不是 HttpResponse
。
这样的调用需要是:
unmarshal[MyType](fromResponseUnmarshsaller)(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
This is the original method call Here you apply response to the returned function.