开发者

Type mismatch using Jedis in Scala

The following code produces four type mismatch errors. Why? In the first and second cases I'm doing a simple comparison with Strings. In the third case I'm assigning false to a var of type Boolean. In the final case I'm merely printing a stack trace!

I am befuddled.

The code:

//return TRUE if logged in
def isLoggedIn(auth: String): Boolean = {
    val jedis = pool.getResource()
    var userid = jedis.get("auth:" + auth)
    var retVal = false
    try {
        if(userid != null) { //error here
            val userAuth = jedis.get("uid:" + userid + ":auth")
            if(userAuth == auth) { // error here
                retVal = true // error here
            }
        }
    } catch {
        case e => e.printStackTrace() //error here
    } finally {
        pool.returnResource(jedis)
        return retVal
    }
}

The error:

开发者_如何学C
[error] type mismatch;
[error]  found   : Unit
[error]  required: Boolean
[error]                     retVal = true // error here
[error]                            ^
[error] type mismatch;
[error]  found   : Unit
[error]  required: Boolean
[error]                 if(userAuth == auth) { // error here
[error]                 ^
[error] type mismatch;
[error]  found   : Unit
[error]  required: Boolean
[error]             if(userid != null) { //error here
[error]             ^
[error] type mismatch;
[error]  found   : Unit
[error]  required: Boolean
[error]             case e => e.printStackTrace() //error here
[error]                                        ^
[error] four errors found

I'm using Jedis 2.0.0 (https://github.com/xetorthio/jedis) to interface with a Redis DB. the Jedis.get() method returns String. I'm using sbt 0.10.1 and scala 2.9.0-1.

What's going on?


Fixed it. Needed to move the return out of the try/catch/finally. Here's the updated code, which compiles just fine. My lingering question is: why can't the return be in the finally?

//return TRUE if logged in
def isLoggedIn(auth: String): Boolean = {
    val jedis = pool.getResource()
    var userid = jedis.get("auth:" + auth)
    var retVal = false
    try {
        if(userid != null) { 
            val userAuth = jedis.get("uid:" + userid + ":auth")
            if(userAuth == auth) { 
                retVal = true 
            }
        }
    } catch {
        case e => e.printStackTrace()
    } finally {
        pool.returnResource(jedis)
    }
    return retVal
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜