[英]Pass generic type to a generic method in scala
I have written a function 我写了一个函数
def getOptional[A](key: String) : Option[A] = {
redisClients.withClient(redisClient =>
redisClient.get[A](key)
)
}
and I am calling it 我在叫它
redis.getOptional[List[Long]](REDIS_PREFIX_PRODUCT_IDS)
but this throws an exception 但这引发了异常
could not find implicit value for parameter parse: com.redis.serialization.Parse[A][error] redisClient.get[A](key)
What is the issue here? 这是什么问题?
The get
requires an additional implicit parameter Parse[A]
. 该get
需要一个附加的隐式参数Parse[A]
。 If there is no Parse[A]
, then it's unclear what to parse, because the type A
is erased at runtime. 如果没有Parse[A]
,则不清楚要解析什么,因为在运行时会擦除类型A
Try this instead: 尝试以下方法:
import com.redis.serialization.Parse
def getOptional[A: Parse](key: String) : Option[A] = {
redisClients.withClient(redisClient =>
redisClient.get[A](key)
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.