0

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:

[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?

Raphael
  • 1,701
  • 15
  • 26
  • 2
    We'd need more context to be able to help you figure out what's going on. For example, what's the surrounding method or expression supposed to return? What line/column is the compiler complaining about? – Alex Cruise Aug 04 '11 at 03:52
  • Sorry. I added more code and details on the error. Thanks. – Raphael Aug 04 '11 at 13:52

1 Answers1

0

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
}
Raphael
  • 1,701
  • 15
  • 26
  • 2
    That won't answer your question but here are some thoughts on why you should not use a `return` statement in a `finally` block. http://stackoverflow.com/questions/48088/returning-from-a-finally-block-in-java By the way you can omit the `return` keyword in Scala. – Mark Jayxcela Aug 04 '11 at 18:24