Reputation: 6262
I am exploring using BDB JE (Berkeley DB Java Edition) with Kotlin. Knowledge of BDB is not necessary to answer this question.
BDB has a method that looks like this:
store.getPrimaryIndex(Int::class.java, "Int", Person::class.java, "Person")
I want to do things generically, so I wrote this
inline fun <reified TModel : Any, reified TKey : Any> getIndex() =
return store.getPrimaryIndex(TKey::class.java, TKey::class.simpleName, TModel::class.java, Model::class.simpleName)
So far so good. I now want to pass this index object to a class, which looks like this:
class ModelStore<TModel, TKey>(index : PrimaryIndex<TKey, TModel>) {
private val index = index
fun get(key : TKey): TModel = index.get(key)
fun put(model : TModel) = index.put(model)
}
But if I try and pass the output from getIndex<User, Int>()
to ModelStore<User, Int>
I get the following error:
Type inference failed: Expected type mismatch: Inferred type is
PrimaryIndex<TModel!, TKey!>!
butPrimaryIndex<TModel, TKey>
was expected.
My question: Can I pass the the index to the ModelStore
? How do I convince the type-system that this is kosher?
Upvotes: 1
Views: 525
Reputation: 33749
The simplest workaround is to cast the types:
ModelStore<User, Int>(getIndex<User, Int>() as PrimaryIndex<User, Int>)
Also I would expect it to work without specifying the types:
ModelStore(getIndex<User, Int>())
Upvotes: 2