Scala:出现错误 "Expression of type Unit doesn't confirm to expected type..."

Scala: getting error as "Expression of type Unit doesn't confirm to expected type..."

我正在为 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))
    }
}

并收到错误消息

Expression of type Unit doesn't confirm to expected type Client

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

您的方法声明为 return 类型 Client 的值。 client.setConnectionTimeout 的 return 类型是 Unit,而不是 Client,因此您不能从您的方法中 return。相反,你应该 return 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
    }
}