I have the following class hierarchy:
sealed class SubscriptionServiceResponse<T>
data class UserRecognized<T>(val recognizedUser: RecognizedUser, val response: T) : SubscriptionServiceResponse<T>()
data class UserNotRecognized<T>(val ignored: Boolean = true) : SubscriptionServiceResponse<T>()
However, I'd prefer UserNotRecognized
to just be an object
- something like:
object UserNotRecognized : SubscriptionServiceResponse()
(The ignored
parameter is just there because I can't make a data class without any parameters).
Is there any way to define an object
as the subtype of a generic sealed class
?
You can use Any
or Any?
, ignoring the generic type for the non-response only. As from your question, I understood that you're not caring about the generic type of your object, so maybe you can not care about it at all
Something like this:
sealed class SubscriptionServiceResponse<T> {
data class UserRecognized<T>(val bool: Boolean) : SubscriptionServiceResponse<T>()
object UserNotRecognized : SubscriptionServiceResponse<Any?>()
}
You can specify the generic type like this:
object UserNotRecognized : SubscriptionServiceResponse<Any>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With