1
votes

Scala newbie running into a problem here:

val cache: LoadingCache[Long, String] = CacheBuilder.newBuilder()
    .maximumSize(1000)
    .expireAfterWrite(10, TimeUnit.MINUTES)
    .build(
      new CacheLoader [Long, String] {
        override def load(key: Long): String = key.toString
      })

And got build time error: Error:(12, 11) type mismatch; found : com.google.common.cache.LoadingCache[Long,String] required: com.google.common.cache.LoadingCache[Long,String] Note: Long <: Long, but Java-defined trait LoadingCache is invariant in type K. You may wish to investigate a wildcard type such as _ <: Long. (SLS 3.2.10) .build(

What's the right fix here? Why the code is wrong? Thanks.

1

1 Answers

1
votes

Try replacing Long with java.lang.Long everywhere (you'll still be able to access it with Long keys thanks to autoboxing). I remember having similar errors with generic types defined in Java when using primitives as type parameters.