architecture-samples icon indicating copy to clipboard operation
architecture-samples copied to clipboard

toString doesn't override dataClass's tostring

Open mobilekosmos opened this issue 4 years ago • 2 comments

The toString function in this class will never be called, at least I don't know where this would be called since this doesn't override the toString() from dataClasses:

sealed class Result<out R> {

    data class Success<out T>(val data: T) : Result<T>()
    data class Error(val exception: Exception) : Result<Nothing>()
    object Loading : Result<Nothing>()

    override fun toString(): String {
        return when (this) {
            is Success<*> -> "Success[data=$data]"
            is Error -> "Error[exception=$exception]"
            Loading -> "Loading"
        }
    }
}

mobilekosmos avatar Oct 23 '21 03:10 mobilekosmos

Hi, i will work on this

haviv1idan avatar Nov 21 '21 17:11 haviv1idan

Hi, try it. This work to me

when(result.javaClass){
    Result.Loading::class.java -> {
        //TODO: Loading
    } 
    Result.Success::class.java -> {
        // TODO: Sub data
    }
    Result.Error::class.java -> {
        // TODO: error sub data
    }
}

tob1developer avatar Nov 25 '21 02:11 tob1developer