首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PartialFunction隐式参数

PartialFunction隐式参数
EN

Stack Overflow用户
提问于 2018-11-27 13:00:17
回答 1查看 259关注 0票数 1

我有一个简单的PartialFunction

代码语言:javascript
复制
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
}

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

代码语言:javascript
复制
...
implicit val ctx: ActorContext
val id: String = msg.id

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

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

如何将我的idMatch函数更改为这样使用它:

代码语言:javascript
复制
...
implicit val ctx: ActorContext
val id: String = msg.id

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-27 13:15:28

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

代码语言:javascript
复制
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))
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53500312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档