如何在Scala中使用JCache?我得到编译器类型错误:找到所需的字符串K [英] How to use JCache in Scala? I get compiler type error: found String required K

查看:94
本文介绍了如何在Scala中使用JCache?我得到编译器类型错误:找到所需的字符串K的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Scala ,并尝试在其中使用javax.cache Scala代码,找不到解决此问题的方法:

val cacheFactory = CacheManager.getInstance.getCacheFactory
val map = new HashMap
val cache = cacheFactory.createCache(map)

def rawSet(key:String, value:Array[Byte]) {
    cache.put(key, value)
}

并且编译器错误是:

错误:类型不匹配
找到:字符串
必需:K
在cache.put(key,value)

修改:
正如Daniel所言,我应该在问题中提到我对应用程序引擎过于关注,因为这似乎非常相关.在这种情况下,解决方案是用Java创建一个小类来执行此特定代码,然后从Scala调用它.

解决方案

尝试:

val cache: Cache[_, AnyRef] = cacheFactory.getCache(new HashMap[String, AnyRef])

甚至Cache[_, _].您可能必须使用类似这样的方法来放置值:

cache.asInstanceOf[HashMap[String,AnyRef]].put(key, value)

我很确定有一种方法可以使用完整的存在语法(Cache[T, AnyRef] forSome { type T })在没有asInstanceOf的情况下进行,但是我不记得怎么做(或找到解释它的网站:). /p>

I'm learning Scala and trying to use javax.cache in Scala code and can't find how to solve this problem:

val cacheFactory = CacheManager.getInstance.getCacheFactory
val map = new HashMap
val cache = cacheFactory.createCache(map)

def rawSet(key:String, value:Array[Byte]) {
    cache.put(key, value)
}

and the compiler error is:

error: type mismatch
found: String
required: K
in cache.put(key, value)

Edit:
As Daniel said, I should have mentioned in the question that I'm over app engine since that seems to be highly relevant. In this case, the solution is to create a small class in Java to do this particular code, and call it from Scala.

解决方案

Try:

val cache: Cache[_, AnyRef] = cacheFactory.getCache(new HashMap[String, AnyRef])

Or even Cache[_, _]. You may have to use something like this to put the values:

cache.asInstanceOf[HashMap[String,AnyRef]].put(key, value)

I'm pretty sure there is a way to do it without asInstanceOf, using the full existential syntax (Cache[T, AnyRef] forSome { type T }), but I can't recall how (or find the site that explains it :).

这篇关于如何在Scala中使用JCache?我得到编译器类型错误:找到所需的字符串K的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆