scala - PartialFunction 隐式参数
问题描述
我有一个简单的 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))
?
解决方案
编译器将始终假定第二个参数列表代表隐式参数列表。您必须以某种方式拆分这两个函数的调用。以下是一些可能性:
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))
推荐阅读
- unity3d - 使用 MRTK2-Unity 使用 Hololens 2 在世界网格上放置全息图/游戏对象
- python - 如何使用 pandas 从 JSON 文件中提取一些信息
- algorithm - 最少会议室数量
- linux - 上一个目录的键绑定(在 Bash 中)
- django - Django模型反应
- python - 如何处理 requests.exceptions.InvalidURL:无法在 python 中解析?
- r - stemCompletion 错误:grep 中的错误(sprintf(“^%s”,w),字典,值 = TRUE):无效的正则表达式,原因 'Missing')''
- javascript - 如何在 Javascript 中获取 pdf 中的选定文本?
- amazon-web-services - 如何使用带有 ubuntu 映像的 docker 卷来访问从 AWS S3 下载的文件?
- android - 是否可以运行 android 模拟器,获取其状态的图像文件,并使用它从无头 linux 服务器模拟鼠标功能?