我已经为使用谷歌集合构建缓存下面的代码:在斯卡拉
val cache = new MapMaker().softValues().expiration(30,
TimeUnit.DAYS).makeComputingMap(
new com.google.common.base.Function[String,Int] {
def apply(key:String):Int ={
1
}
})
而且我收到以下错误消息:
error: type mismatch;
found : java.lang.Object with
com.google.common.base.Function[java.lang.String,Int]{ ... }
required: com.google.common.base.Function[?, ?]
new com.google.common.base.Function[String,Int] {
^
我很奇怪,为什么类型不匹配?
实际的代码是:
import com.google.common.collect.MapMaker
trait DataCache[V] {
private val cache = new MapMaker().softValues().makeComputingMap(
new com.google.common.base.Function[String,V] {
def apply(key:String):V = null.asInstanceOf[V]
})
def get(key:String):V = cache.get(key)
}
亲切的问候, 阿里
PS - 我使用谷歌的集合V1
不行的,现在我得到这个错误:需要 类的类型,但com.google.common.base.Function [_,_]发现 新com.google.common.base.Function [ _,_] 我也试过-explaintypes,其实输出真的很难理解: java.lang.Object与com.google.common.base.Function [_,_]
2010-01-29 13:54:59
@Ali:你似乎只是尝试了第一个建议。我现在粘贴了第二个建议的完整代码。另外,请将'-explaintypes'的输出添加到您的问题中。 – 2010-01-29 14:06:14
其实输出很大,我添加了实际的代码,其中一个可以复制粘贴来重现错误消息。请让我知道,如果有什么我会提供。 – 2010-01-29 16:16:19