xun su

05/18/2023, 12:57 AM
Hi, say after send a request , the response have two type , when success
data class Success(
    var code: Int, // maybe 0
    var content: String,
) {}
while fail:
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 ?

Alejandro Rios

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