vendredi 23 avril 2021

Why does Kotlin (community) encourage reflection in pattern matching

Almost every Kotlin Android tutorial or project intensively uses when-is constructions like the following

val response : Response = //...
when(response){
   is Success   -> useResponseData(response.data)
   is Cancelled -> handleCancellation()
   is Error     -> handleError(response.errorCode) 
}

As far as I understand, the usage of reflection is not justified in most cases and should be avoided. I suppose that my example is that particular case when the use of reflection is justified, provided that the Response is sealed and most likely the set of its subclasses will never change. Am I correct?

Is such construction in my example absolutely "clean", or rather the compromise for the sake of code simplicity?





Aucun commentaire:

Enregistrer un commentaire