问题严格来说是
Scala语法,尽管它包含来自akka的一些代码(作为示例).
我对Scala很新.深入研究akka的源代码我想出了以下非常奇怪的方法:
def transform[C] (f: ExecutionContext ⇒ Materializer ⇒ Future[B] ⇒ Future[C]): Unmarshaller[A,C] = Unmarshaller.withMaterializer { implicit ec ⇒ implicit mat ⇒ a ⇒ f(ec)(mat)(this(a)) }
其中Unmarshaller.withMaterializer定义为
def withMaterializer[A,B](f: ExecutionContext ⇒ Materializer => A ⇒ Future[B]): Unmarshaller[A,B]
这里发生了什么?什么是可怕的函数f:ExecutionContext => Materializer =>未来[B] =>未来[C].对我来说似乎更奇怪的是隐含的序列:隐含的ec =>隐含的mat => a => f(ec)(mat)(this(a))尽管withMaterializer根本没有隐含参数.
这种序列中的隐含意味着什么?
解决方法
f:ExecutionContext => Materializer =>未来[B] => Future [C]只不过是一个curried函数,所以你把它称为f(ec)(mat)(this(a))有多个参数列表(好吧,技术上参数列表不属于同一个函数,不像def f(…)(…),但那些是细节).换句话说,f可以写成:
f: ExecutionContext => { Materializer => { Future[B] => Future[C] } }`
(返回函数的函数,返回另一个函数)
现在,如果你看一下f(ec)(mat)(this(a)),就会调用this(a),它就在变换之上定义:
def apply(value: A)(implicit ec: ExecutionContext,materializer: Materializer): Future[B]
(这个(a)只是对this.apply(a)的调用).现在apply有两个隐式参数,即ec:ExecutionContext和Materializer:Materializer,所以要像这样调用它(a)你需要两个隐式值.这正是定义隐含的ec⇒隐式mat⇒a⇒f(ec)(mat)(this(a))的意思.它将ec和mat声明为所有嵌套函数体的含义,因此这个(a)可以拾取它们.另一种可能性是写:
ec ⇒ mat ⇒ a ⇒ f(ec)(mat)(this(a)(ec,mat))