question on sealed class can i have sealed class w...
# announcements
c
question on sealed class can i have sealed class with following possibilty for example have a sealed class name Response with following data class Header,Body and Error often ill have Header and Body or Header and Error or Body or Error alone if for example i declare SealeadClass<H,B> if something gone wrong cant pass SealedClass.Error() as response.
e
Why would you pass Header and body as generic types?
Copy code
data class Response(val header: Header, val body: Body?, val error: Error?)
should be good enough?
c
is it possible to do that in sealed class?
e
why do you need a sealed class?
c
the reviewer wants to be in sealed class but i have no clue how to do with type specified in Response .
as if i mention Response<String> , need to return string but what if something fails?
ill see this later
m
You want an Either type that holds either an error or a valid response. Arrow library has it in core with helpers for comprehension or yoy can write your own.
d
The key is the
Nothing
type:
Copy code
sealed class Response<out T> {
    data class Value<T>(val v: T) : Response<T>()
    data class Error(val err: Throwable) : Response<Nothing>()
}
c
@diesieben07 how do you return header+body?
with type ofcourse
d
So you have two classes (Header and Body) and you want a type that's either Header or Body?
c
have mentioned in the question already Header and Body both are different type
can say two types
d
Yes, you have mentioned that. But I do not understand your actual question.
c
what you didnt understand?
often ill have Header and Body or Header and Error or Body or Error alone
Header is optional
d
Copy code
sealed class Message {

    data class Header(val headerValue: String): Message()
    data class Body(val bodyValue: String): Message()
    data class Error(val error: Throwable): Message()

}
Then you can use
List<Message>
to represent all your cases
c
there is a clause in that , there is generics involved in this for Header<T> Body<T>
d
Copy code
sealed class Message<out HT, out BT> {

    data class Header<out HT>(val headerValue: HT): Message<HT, Nothing>()
    data class Body<out BT>(val bodyValue: BT): Message<Nothing, BT>()
    data class Error(val error: Throwable): Message<Nothing, Nothing>()

}

fun main() {
    val messages = listOf<Message<String, Int>>(
        Message.Header("hello"),
        Message.Body(15),
        Message.Error(Exception("oops"))
    )
    for (message in messages) {
        when (message) {
            is Message.Header -> println("Header: ${message.headerValue}")
            is Message.Body -> println("Body: ${message.bodyValue}")
            is Message.Error -> println("error :(")
        }
    }
}
c
there is one more clause which im facing atm have a function
responseSent:Array<Result<RsH,RsB>>
Result is a sealed class as you see im mentioning the type RsH,RsB , so if something fails can't return Result<Nothing.Nothing>.Error
d
Yes, thats why in my declaration
HT
and
BT
are declared as
out
. Because
Nothing
is a subtype of everything, in that case
Error
can substitute for any
Message
, regardless of it's
HT
and
BT
.
c
btw have done all the things mentioned earlier excluding Nothing.
btw why not go with normal class, why sealed class that didn't make sense
for this use case
d
Sealed classes allow exhaustiveness checking by the compiler. If you check my
when
example above, the compiler can check that you have covered all cases, if the class is
sealed
.
c
yes , i agree but having normal class makes the state management for this use case pretty easy right?
d
in what way? Please elaborate
c
can easily set the data to a normal class instance 🙂 and have a generic type. and can access/share upon it . Instead of sealed class , not considering pros of it.
d
You can do just the same with a sealed class, just that your "normal class" now has this sealed class as a superclass
everything else stays the same
c
i dont like inheritance
m
inheritance is amazing tho
c
tbh have forgot about Nothing
just remembered , thanks alot
Nothing is so helpful 🙂
lesson learnt never forget
Nothing
although felt doing with sealed class is more like working with prototype in javascript , not saying they are same in anyway but having a snake around your neck.
to do something simple we are going with alot of hooks.
state management with no simplicity are crazy.