Ishank Gulati
Ishank Gulati

Reputation: 655

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?

Upvotes: 0

Views: 58

Answers (1)

Andrey Tyukin
Andrey Tyukin

Reputation: 44957

The get requires an additional implicit parameter Parse[A]. If there is no Parse[A], then it's unclear what to parse, because the type A is erased at runtime. Try this instead:

import com.redis.serialization.Parse
def getOptional[A: Parse](key: String) : Option[A] = {
  redisClients.withClient(redisClient =>
    redisClient.get[A](key)
  )
}

Upvotes: 2

Related Questions