我正在尝试将Lettuse(Redis客户端之一)的异步通信java示例代码转换为scala,但遇到编译错误,称参数表达式的类型不兼容。
这是我的代码
def RunAsync() = {
val redisClient = new RedisClient(RedisURI.create(connURL))
val connection = redisClient.connectAsync()
connection.setAutoFlushCommands(false)
val futures = Lists.newArrayList[RedisFuture[_]]()
for(lc <- Range(0, logCount))
{
futures.add(connection.set(logContents(lc).key, logContents(lc).value))
}
connection.flushCommands()
val convFutures = futures.toArray(new Array[RedisFuture[_]](futures.size()))
val result = LettuceFutures.awaitAll(10L, TimeUnit.SECONDS, convFutures : _*)
connection.close()
redisClient.shutdown()
}
这是我在编译时收到的错误信息
Error:(67, 31) no type parameters for method toArray: (x$1: Array[T with Object])Array[T with Object] exist so that it can be applied to arguments (Array[com.lambdaworks.redis.RedisFuture[_]])
--- because ---
argument expression's type is not compatible with formal parameter type;
found : Array[com.lambdaworks.redis.RedisFuture[_]]
required: Array[?T with Object]
Note: com.lambdaworks.redis.RedisFuture[_] >: ?T with Object, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ?T with Object`. (SLS 3.2.10)
val convFutures = futures.toArray(new Array[RedisFuture[_]](futures.size()))
^
经过几个小时的谷歌搜索,我可以找到一些参考资料来帮助了解我的情况,但没能找到解决方案。有人能告诉我解决这个问题的最佳方法吗?
为什么要使用java列表?试试这个:
val convFutures = Range(0, logCount).map { lc =>
connection.set(logContents(lc).key, logContents(ls).value))
}.toArray
logContents
btw是什么类型?可能会让它变得更简单。。。
此外,看起来您根本不需要.toArray
。scala中的变量参数是Seq
,而不是Array
。