I’m working on migrating my thread-based code to coroutines.
I previously had a List called RedisList which preformed all of its operations against a redis database over a network connection. Now that I’m migrating to the Lettuce library which supports couroutines, I’d like all of my RedisList functions to suspend. However, the List interface does not support suspend functions.
Maybe I’m still struggling a little with understanding this migration. Since I can’t override List methods with the suspend keyword, now I feel like I need to create my own SuspendableList interface from scratch:
interface SuspendCollection<E> {
suspend fun size(): Int
suspend fun contains(element: E): Boolean
suspend fun containsAll(elements: SuspendCollection<E>)
suspend fun isEmpty(): Boolean
suspend fun iterator(): SuspendIterator<E>
}
interface SuspendList<E>: SuspendCollection<E> {
suspend fun get(index: Int): E
suspend fun indexOf(element: E): Int
suspend fun lastIndexOf(element: E): Int
suspend fun listIterator(): SuspendListIterator<E>
suspend fun listIterator(index: Int): SuspendListIterator<E>
suspend fun subList(fromIndex: Int, toIndex: Int): SuspendList<E>
}
interface SuspendIterator<E>
interface SuspendListIterator<E>: SuspendIterator<E>
This of course feels crazy, because then I can no longer use any functions for List and need to create versions that are for SuspendList.
What am I missing?