https://kotlinlang.org logo
Title
x

xun su

05/18/2023, 12:57 AM
Hi, say after send a request , the response have two type , when success
@Serializable
data class Success(
    var code: Int, // maybe 0
    var content: String,
) {}
while fail:
@Serializable
data class Fail(
    var code: Int, // maybe 1
    var err: String,
) {}
I wanna know what is the elegant way to handle the two situation?
val res:Response = sendRequest<what should I put here?>()
// and how should I define Response?
if(res instanceOf Success){...} else if (res instanceOf Fail){...}
and is the code above a proper way ?
a

Alejandro Rios

05/18/2023, 3:35 AM
I think you can use railway oriented programming here