【问题标题】:PartialFunction implicit parametersPartialFunction 隐式参数
【发布时间】:2019-04-29 05:33:19
【问题描述】:

我有一个简单的 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
}

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

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

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

如您所见,它希望 ctx 作为第二个参数而不是隐式

如何更改我的 idMatch 函数以像这样使用它:

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

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

?

【问题讨论】:

  • 为什么这是一个偏函数?看起来像一个

标签: scala akka implicit partial-functions


【解决方案1】:

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

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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    相关资源
    最近更新 更多