Function 如何调用从另一个函数返回的函数?
我正在尝试调用函数从封送处理<代码>解组功能已描述。回应是 考虑3种代码变体: 弗斯特 第二 第三 为什么我的第三个代码变体没有编译,而前两个是可以工作的?编译器返回:Function 如何调用从另一个函数返回的函数?,function,scala,call,spray,Function,Scala,Call,Spray,我正在尝试调用函数从封送处理解组功能已描述。回应是 考虑3种代码变体: 弗斯特 第二 第三 为什么我的第三个代码变体没有编译,而前两个是可以工作的?编译器返回: [error] found : spray.http.HttpResponse [error] required: spray.httpx.unmarshalling.FromResponseUnmarshaller[MyType] [error] (which expands to) spray.httpx.unma
[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)
是否有一种方法可以调用
unmarshal
返回的函数,然后创建临时变量或直接调用apply
方法 该函数的签名是(从您的链接):
因此,T
需要隐式的证据来证明它有一个来自responseunmarshaller的。签名实际上编译为如下内容:
def unmarshal[T](implicit evidence$1: FromResponseUnmarshaller[T]): HttpResponse ⇒ T
这意味着,unmarshal
函数实际上还包含一个隐式参数,该参数应将MyType
转换为HttpResponse
在第一个示例中,调用val func=unmarshal[MyType]
,这使编译器为您插入隐式。在第三个例子中
unmarshal[MyType](response)
response
取隐式参数的位置,该参数应该是responseUnmarshaller
中的,而不是HttpResponse
这种呼吁需要:
unmarshal[MyType](fromResponseUnmarshsaller)(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
This is the original method call Here you apply response to the returned function.
第二个和第三个是相同的。您是否错误地粘贴了错误的内容?我(还)没有写一行scala代码,但我想至少(unmarshal[MyType])(response)
应该使用合理的(FP)语言。对于错误的第三个示例,我感到抱歉。问题已更新。2Carsten,您通常是对的,我已经尝试过了,但编译器抱怨出现了相同的错误:(很抱歉……我已经可以说我喜欢scala;)我在第def unmarshal[T:fromResponseUnmarshaler]:HttpResponse行中没有看到任何隐式参数⇒ T
fromunmarshal
函数源。请为unmarshal
函数添加更多关于隐式参数的详细信息好吗?@Cherry,该参数通过定义[T:fromResponseUnmarshaler]
包含在其中。尝试在REPL上创建一个函数,如def[T:List]=0
,您将看到REPL将其定义为f[T](隐式证据$1:List[T])Int
。有关更多信息,请查看@dcastro的链接谢谢。似乎无法从call语句中跳过隐式上下文绑定参数:(
def unmarshal[T: FromResponseUnmarshaller]
def unmarshal[T](implicit evidence$1: FromResponseUnmarshaller[T]): HttpResponse ⇒ T
unmarshal[MyType](response)
unmarshal[MyType](fromResponseUnmarshsaller)(response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^
This is the original method call Here you apply response to the returned function.