I'm getting an error when trying to override a fun...
# announcements
d
I'm getting an error when trying to override a function that's implemented by delegation (context in thread)
Here's what I'm trying to do:
Copy code
private val cache: LoadingCache<Long, String> = Caffeine.newBuilder()
    .build { it.toString() }

object Foo: LoadingCache<Long, String> by cache {
    override fun get(key: Long): String {
        return cache.get(key)!!
    }
}
The error I get is:
Copy code
Platform declaration clash: The following declarations have the same JVM signature (get(Ljava/lang/Object;)Ljava/lang/Object;): 
public open fun get(key: Long): String? defined in Foo
public open fun get(key: Long): String defined in Foo
The interface is a part of the
caffeine
cache library (written in Java). The relevant declaration is:
Copy code
public interface LoadingCache<K, V> extends Cache<K, V> {
  @Nullable
  V get(@NonNull K key);
}
Is there something that I'm doing wrong?
b
override fun get(key: Long): String? {
d
Also, if I change the return type of the overrided function from
String
to
String?
I get the same
Platform declaration clash
error
b
Ah 🤔
d
same if i drop
override
from the non-null return type
Not sure it works with
override
, though
d
'@JvmName' annotation is not applicable to this declaration
¯\_(ツ)_/¯
e
I think that's one reason why some people don't like the implementation by delegation feature 😛
Well, apparently the only way is to use delegation with manual calls to
cache
d
Oh interesting. I didn't realize that people didn't really like it. Haven't used it much though.
And yeah I discovered that too. I ended up just working with the cache instance directly; all I wanted to do was try to make it non-null but going through all the trouble to get it there didn't outweigh just handling it where I call
get