How to build generic query function without having ERROR: "Left-hand side of a callable reference cannot be a type parameter"
urosjarc opened this issue · 2 comments
This is known problem with kotlin: https://youtrack.jetbrains.com/issue/KT-14591/Support-type-parameter-on-the-left-hand-side-of-callable-references
My question is how to best mitigate this kind of problem when trying to query with generic types:
private inline fun <reified T : MyEntity<T>> get_many(ids: List<Id<T>>): List<T> {
return this.col<T>().find(T::id `in` ids).toList()
}
Hi @urosjarc ,
I don't think that this is really an KMongo issue, even not an Kotlin issue. To me it's more like an 'use case' of generics on Kotlin that can be solved by arranging your classes and/or generics parameters usage.
Said the above, it's very possible that I don't understand correctly what you are trying to do here, but my best guess is that your MyEntity
class is a base class for other specialized data containers and you are trying to get an list of MyEntity's documents whose id value is contained in a list of Id 's. So I think that your function has to be more like this:
private fun getMany(ids: List<Id<MyEntity>>): List<MyEntity> {
return col<MyEntity>.find(MyEntity::_id `in` ids).toList()
}
best regards
Oh off course kmongo uses @SerialName
to create _id
label so it doesn't matter from which object field is sourced from (parent or child). Thank you this solves my issue.