无法应用参数化的咖喱函数

时间:2019-06-09 19:38:16

标签: 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)
  } 

但这不起作用。

1 个答案:

答案 0 :(得分:3)

要使res1 { case status: ServerStatus => status.health should be(ServersOffline) }工作,请尝试通过向T提供类型参数expectMsgPF[T]来帮助编译器

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

这使res1确实是类型

的函数
PartialFunction[Any, Assertion] => Assertion