PartialFunction隐式参数

HoT*_*icE 1 scala implicit akka partial-functions

我有一个简单的PartialFunction

type ChildMatch = PartialFunction[Option[ActorRef], Unit]

def idMatch(msg: AnyRef, fail: AnyRef)(implicit ctx: ActorContext): ChildMatch = {
    case Some(ref) => ref forward msg
    case _ => ctx.sender() ! fail
}
Run Code Online (Sandbox Code Playgroud)

但是当我试图使用它时 - 编译器需要这样的声明:

...
implicit val ctx: ActorContext
val id: String = msg.id

idMatch(msg, fail)(ctx)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)

你可以看到它想要ctx作为第二个参数而不是隐含的

我怎么能改变我的idMatch函数使用它像这样:

...
implicit val ctx: ActorContext
val id: String = msg.id

idMatch(msg, fail)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)

Kol*_*mar 6

编译器将始终假定第二个参数列表代表隐式参数列表.您必须以某种方式拆分这两个函数的调用.以下是一些可能性:

idMatch(msg, fail).apply(ctx.child(id))

val matcher = idMatch(msg, fail)
matcher(ctx.child(id))

// Provides the implicit explicitly from the implicit scope
idMatch(msg, fail)(implicitly)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)