Scala Guava类型不匹配问题 [英] Scala Guava type mismatch issue
本文介绍了Scala Guava类型不匹配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Guava缓存实现一个简单的用例,但面临一些问题,如下所示:
I am trying to implement a simple usecase using Guava caching but facing some issues as shown below:
case class Person(x:Int, y:String)
val db = Map(1 -> Person(1,"A"), 2 -> Person(2,"B"), 3 -> Person(3,"C"))
val loader:CacheLoader[Int,Person] = new CacheLoader[Int,Person](){
def load(key: Int): Person = {
db(key)
}
}
lazy val someData = CacheBuilder.newBuilder().expireAfterWrite(60, MINUTES).maximumSize(10).build(loader)
someData.get(3)
我遇到的错误与我无法弄清的类型有关
The error I am getting is related to types which I am not able figure out
scala> someData.get(3)
<console>:24: error: type mismatch;
found : Int(3)
required: Int
someData.get(3)
有人可以对可能出现的问题提出建议吗?
Can someone advice on what can be the issue.
推荐答案
这是Java的使用站点协方差注释的常见问题.
That's a common issue with Java's use-site covariance annotations.
这适用于scala 2.12.4和番石榴24.1:
This here works with scala 2.12.4 and guava 24.1:
import com.google.common.cache._
import java.util.concurrent.TimeUnit._
object GuavaCacheBuilderTypeProblem {
case class Person(x:Int, y:String)
val db = Map(1 -> Person(1,"A"), 2 -> Person(2,"B"), 3 -> Person(3,"C"))
val loader: CacheLoader[java.lang.Integer, Person] =
new CacheLoader[java.lang.Integer, Person](){
def load(key: java.lang.Integer): Person = {
db(key)
}
}
lazy val someData = CacheBuilder
.newBuilder()
.expireAfterWrite(60, MINUTES)
.maximumSize(10)
.build[java.lang.Integer, Person](loader)
someData.get(3)
}
有类似错误的答案:
这篇关于Scala Guava类型不匹配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文