Litote / kmongo

[deprecated] KMongo - a Kotlin toolkit for Mongo
https://litote.org/kmongo/
Apache License 2.0
781 stars 75 forks source link

Can CoroutineCollection<T>.replaceOne(replacement, options) be made non reified? #408

Open jfontsaballs opened 1 year ago

jfontsaballs commented 1 year ago

I'd would like to ask why this function is reified, and whether it can be made non-reified?

https://github.com/Litote/kmongo/blob/a64be58cf52e42b60e057317ff7ff68d5429ea17/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/CoroutineCollection.kt#L1856

Concretely, maybe this line can be rewritten? If using T::class can be avoided, there's no need for the function to be reified, thus allowing it to be used in non-inline generic functions.

https://github.com/Litote/kmongo/blob/a64be58cf52e42b60e057317ff7ff68d5429ea17/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/CoroutineCollection.kt#L1859

// Possible rewrite:
replaceOneById(extractId(replacement, replacement::class), replacement, options)

The save function has, I believe, similar behavior if the involved class has an _id property, without it being reified.

https://github.com/Litote/kmongo/blob/a64be58cf52e42b60e057317ff7ff68d5429ea17/kmongo-coroutine-core/src/main/kotlin/org/litote/kmongo/coroutine/CoroutineCollection.kt#L1262

My use case is as follows: we are building a internal library where we want to write a class that handles certain operations with a MongoDB database, but the concrete type can vary between different usages of the library. It is impossible to write this class using inline functions and reified generics, so we must use non-reified functions.