在 Scala 中使用 Jedis 时类型不匹配

发布于 2024-11-28 02:08:57 字数 1636 浏览 1 评论 0原文

以下代码产生四个类型不匹配错误。为什么?在第一个和第二个例子中,我与字符串进行了简单的比较。在第三种情况下,我将 false 分配给 Boolean 类型的 var。在最后一种情况下,我只是打印堆栈跟踪!

我很困惑。

代码:

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

错误:

[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

我正在使用 Jedis 2.0.0 (https://github.com/xetorthio/jedis) 与 Redis DB 进行交互。 Jedis.get() 方法返回String。我正在使用 sbt 0.10.1 和 scala 2.9.0-1。

这是怎么回事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-12-05 02:08:57

修好了。需要将 return 移出 try/catch/finally。这是更新后的代码,编译得很好。我挥之不去的问题是:为什么return不能放在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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文