https://kotlinlang.org logo
#announcements
Title
# announcements
d

daphillips

10/13/2020, 3:22 PM
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

Brett Best

10/13/2020, 3:24 PM
override fun get(key: Long): String? {
d

daphillips

10/13/2020, 3:24 PM
Also, if I change the return type of the overrided function from
String
to
String?
I get the same
Platform declaration clash
error
b

Brett Best

10/13/2020, 3:24 PM
Ah 🤔
d

daphillips

10/13/2020, 3:25 PM
same if i drop
override
from the non-null return type
Not sure it works with
override
, though
d

daphillips

10/13/2020, 3:41 PM
'@JvmName' annotation is not applicable to this declaration
¯\_(ツ)_/¯
e

edrd

10/13/2020, 4:56 PM
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

daphillips

10/13/2020, 4:57 PM
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
2 Views