How to implement generic function of Interface in Kotlin?

Viewed 2448

There is an interface CacheManager.java from shiro.

package org.apache.shiro.cache;

public interface CacheManager {
    <K, V> Cache<K, V> getCache(String var1) throws CacheException;
}

I wrote a class DefaultCacheManager.kt that implemented the CacheManager.

open class DefaultCacheManager : CacheManager {
    @Throws(CacheException::class)
    override fun getCache(name: String): Cache<Any, Any> {
        return InnerCache(name)
    }
} 
class InnerCache(var name: String) : Cache<Any, Any> {
//.... omitted ....
}

The code above would cause compilation error.

Conflicting overloads: 
@Throws public open fun getCache(name: String): Cache<Any, Any> defined in com.yootai.oms.permission.DefaultCacheManager, 
public abstract fun <K : Any!, V : Any!> getCache(p0: String!): Cache<K!, V!>! defined in org.apache.shiro.cache.CacheManager

Then, I modified my code below.

override fun <K : Any?, V : Any?> getCache(name: String): Cache<K, V> {
        return InnerCache(name)
}

But, it would give me a compilation error info.

Type mismatch.
Required: Cache<K,V>
Found: InnerCache

How can I fix the problem? Please help me.

1 Answers
Related