allan.conda
03/24/2021, 9:52 AMsealed class Error(val code: String) {
object Type1 : Error("type1")
data class Type2(val someParam: String) : Error("type2")
}
// somewhere else in code
if (error.code == Type2.code) { }
Is there a best practice for achieving a check like above with a data class sealed class?Roukanken
03/24/2021, 9:56 AMcode
really
since you can do the check with is
eg, either error is Error.Type2
or do it with when
when(error) {
is Error.Type1 -> {}
is Error.Type2 -> {}
}
Mykola Gurov
03/24/2021, 9:57 AMif (error is Type2)
?Roukanken
03/24/2021, 9:57 AMallan.conda
03/24/2021, 9:57 AMallan.conda
03/24/2021, 9:57 AMerror
in error.code
is of different typeMykola Gurov
03/24/2021, 9:58 AMval
, so it won't be other than "type2"
in your exampleallan.conda
03/24/2021, 9:58 AMMykola Gurov
03/24/2021, 9:58 AMallan.conda
03/24/2021, 9:59 AMif (error.code == Type2("").code)
but it looks hackyRoukanken
03/24/2021, 10:00 AMMykola Gurov
03/24/2021, 10:00 AMerror
isn't an instance of Error
?allan.conda
03/24/2021, 10:01 AMSomeApiError
Mykola Gurov
03/24/2021, 10:01 AMallan.conda
03/24/2021, 10:02 AM