How to define object inside sealed generic hierarc...
# getting-started
a
How to define object inside sealed generic hierarchy?
Copy code
sealed class PaginationViewState<T> {
    object LoadingFirstPage: PaginationViewState<T>()//doesn't work
    object LoadingPage: PaginationViewState<T>()//doesn't work

    data class DataLoaded<T>(val data: List<T>): PaginationViewState<T>()
    data class DataLoadedFromCache<T>(val data: List<T>): PaginationViewState<T>()
    data class Failure<T>(val e: Throwable): PaginationViewState<T>()
}
m
You need to specify a
T
for your objects.
By the way, after Kotlin 1.1, you don't need to nest the classes and objects inside the sealed class. They just have to be in the same file.
a
no, type parameters not allowed
m
The
T
has to be an actual class/interface here:
object LoadingFirstPage : PaginationViewState<T>()
No type parameters allowed? What do you mean? You have specified
PaginationViewState
with a type parameter.
a
not allowed in this statement
object LoadingFirstPage<T>: PaginationViewState<T>()
m
That doesn't work because there is only one instance of
LoadingFirstPage
. It can't take parameters.
Maybe
LoadingFirstPage
should be a class then?
a
that is what I did😀
m
It looks like that should be the correct solution for you looking at your example.
T
is the type of data that the view state holds, so
LoadingFirstPage
and
LoadingPage
are classes, not objects.
I see you are writing on the StackOverflow post that using
Any
is what you were looking for. I really don't think you should do that. You are losing type information that you don't have to lose.
👍 1
a
yes, it is meaningless since I can't define class with desired type
PaginationViewState<AdModel> =PaginationViewState.LoadingFirstPage
. I'll stick with regular classes probably
m
You can still have (and you should have) a sealed class hierarchy though.