在 Scala 中使用 Jedis 时类型不匹配
以下代码产生四个类型不匹配错误。为什么?在第一个和第二个例子中,我与字符串进行了简单的比较。在第三种情况下,我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修好了。需要将
return
移出 try/catch/finally。这是更新后的代码,编译得很好。我挥之不去的问题是:为什么return
不能放在finally中?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 thereturn
be in the finally?