Scala:得到错误"单位类型的表达式不能确认预期的类型......"

时间:2018-05-25 18:56:03

标签: scala jersey-client

我正在设置Client的连接超时,如

def newClient(host: String): Client = asyncHttpClient match {
    case true => {
        import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
        AhcHttpClient.create()
    }
    case _ => {
        import com.sun.jersey.api.client.Client
        val client: Client = Client.create()
        client.setConnectTimeout(5000)
        //Or client.setConnectTimeout(Int.box(5000))
    }
}

并收到错误消息

  

类型单位的表达式未确认预期类型客户端

有人可以帮助理解问题并正确传递整数值吗?

1 个答案:

答案 0 :(得分:4)

声明您的方法返回类型为Client的值。 client.setConnectionTimeout的返回类型为Unit,而不是Client,因此您无法从方法中返回该类型。相反,您应该返回client

def newClient(host: String): Client = asyncHttpClient match {
    case true => {
        import org.sonatype.spice.jersey.client.ahc.AhcHttpClient
        AhcHttpClient.create()
    }
    case _ => {
        import com.sun.jersey.api.client.Client
        val client: Client = Client.create()
        client.setConnectTimeout(5000)
        client
    }
}