Hello, I have a result-pattern-like generic sealed...
# getting-started
g
Hello, I have a result-pattern-like generic sealed class, I'd like to define objects that represent possible values that are independent of the type specified, is it possible?
Copy code
sealed class State<out T : Any>
object Loading : State</* Type isn't relevant here */>()
data class Loaded<T : Any>(data: T) : State<T>()
There's no reason for
Loading
not to be an object in this context, neither is there any reason for it to specify a type. Changing the variance to contravariance and making it an
Any
is a design flaw in this scenario. Is there any convenient alternative to what I'm trying to accomplish here?
s
Copy code
object Loading : State<Nothing>()
?
2
g
That did work. Didn't cross my mind
Nothing
was a possibility
s
Yeah, there’s no Java equivalent to
Nothing
, so I tend to forget about it too, but it’s really useful sometimes
m
Yes, that’s the correct usage of
Nothing
! It works because it’s a subtype of any type (the opposite of Any) but can’t be instantiated.
g
The beauty of a good type system