我正在为scala 2.10.1中的一些实体编写通用缓存。目前,我正在使用google番石榴的CacheBuilder,因为在scala生态系统中没有太多选项。scala 2.10类型不匹配使用google guava的CacheBuilder
代码:
trait CachedEntity[E <: KeyedEntity[K],K] {
def lookup(id:K):E
def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption
val elemCache = CacheBuilder.newBuilder().maximumSize(10).expireAfterWrite(1,TimeUnit.MINUTES).build(
new CacheLoader[K,E] {
def load(key:K) = {
println("Looking Up key:" + key + "in Class:" + this.getClass.getName)
lookup(key)
}
}
)
}
trait LongKeyed[E<: KeyedEntity[Long],Long] extends CachedEntity[E,Long]
然而,SBT抛出错误:
[error] KEHCaching.scala:16: type mismatch;
[error] found : id.type (with underlying type K)
[error] required: Object with K
[error] def getElem(id:K):Option[E] = Try(elemCache.get(id)).toOption
[error] ^
[error] one error found
任何想法?即使我后添加K <:对象是这样的:
trait CachedEntity[E <: KeyedEntity[K],K <:Object] {
我得到这个错误
[error] KEHCaching.scala:27: type arguments [E,Long] do not conform to trait CachedEntity's type parameter bounds [E <: org.squeryl.KeyedEntity[K],K <: Object]
[error] trait LongKeyed[E<: KeyedEntity[Long],Long] extends CachedEntity[E,Long]
[error] ^
[error] one error found
如果代码片段在语法上是正确的,那将是最好的。代码中的两个连续时段('.')无法编译。此外,你的代码_suggests_,但没有说明'K'和'E'是绑定在某个封闭结构中的类型参数。你的正确和完整的代码片段,以便人们复制和修改你的原代码? – 2013-04-24 15:55:12
对不起。也使用类声明更新了代码。 – adivis 2013-04-25 07:43:21