Search code examples
scalajersey-client

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


I am setting the connection timeout for Client like

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))
    }
}

and getting the error message

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

Could someone help to understand the problem and properly pass the integer value?


Solution

  • Your method is declared to return a value of type Client. The return type of client.setConnectionTimeout is Unit, not Client, so you can't return that from your method. Instead you should 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
        }
    }