scala - 无法应用参数化柯里化(Currying)函数

标签 scala currying

我有一个函数,其定义如下:

def expectMsgPF[T](max: Duration = Duration.Undefined, hint: String = "")(f: PartialFunction[Any, T]): T = {

当我这样调用它时:

val res1 = listener1.expectMsgPF(1.second) 

res1 是一个函数吗?

我想写如下:

 val res1 = listener1.expectMsgPF(1.second) _
 val res2 = listener2.expectMsgPF(1.second)
 Then("it should contain `Kafka and SAP are offline`")
 res1 {
    case status: ServerStatus =>
    status.health should be(ServersOffline)
  } 

但是它不起作用。

最佳答案

制作 res1 { case status: ServerStatus => status.health should be(ServersOffline) }工作,尝试通过提供类型参数 T 来帮助编译器。至expectMsgPF[T]像这样

val res1 = listener1.expectMsgPF[Assertion](1.second) _

这使得res1确实是一个类型的函数

PartialFunction[Any, Assertion] => Assertion

关于scala - 无法应用参数化柯里化(Currying)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517834/

相关文章:

scala - 灵活的查询过滤器作用于任意列

Python 相当于 Scala 的 lazy val

scala - 关于编译期间未处理的类型类的警告

python - 如何在给定参数位置柯里化(Currying)函数

haskell - 将值绑定(bind)到非第一个参数

scala - maven项目编译问题

Scala将可变参数传递给另一个接受可变参数的函数

haskell - curry (==) 是如何工作的?

functional-programming - 柯里化(Currying)的真实用例是什么?

f# - 您如何在 F# 或任何函数式语言中对第二个(或第三个、第四个、...)参数进行 curry?