Hi Folks, I wanted advice regarding structuring a ...
# general-advice
a
Hi Folks, I wanted advice regarding structuring a sealed class hierarchy with generics, Lets say I have a sealed class with 2 child classes and one of them needs to be generic, should I make the parent class also generic or only the child class as generic. Basically which one is preferred ->
Copy code
// Sealed class without generics
sealed class Result

// Subclass with a generic type
data class Success<T>(val data: T) : Result()

// Subclass without a generic type
object Loading : Result()
Or
Copy code
sealed class Result<T>

class Success<T>(val data: T) : Result<T>()

// Some other subclass that doesn't need T can be defined as
class Error(val message: String) : Result<Nothing>()
j
Usually the second approach will be more useful and convenient. With the first approach, anything returning the base
Result
will not provide any information about the contained data, and you'll have to do annoying runtime checks to work around it.
👍 2