Mehdi Haghgoo
11/15/2021, 9:07 PMoverride fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
This is from the template Login Activity in Android Studio. What is Success<*> and what is "Success[data=$data]"?Luke
11/15/2021, 9:30 PMthis
can be an instance of either Success, or Error. You are checking whether you have an instance of one or the other. The thing is that your class is generic. You can have a Success<Int>
, or a Success<String>
for example. In this case, you don't care whether you have Int or String, you want to match any Success, hence the wildcard <*>
Mehdi Haghgoo
11/15/2021, 9:45 PMPaul Woitaschek
11/15/2021, 11:01 PMVahalaru
11/16/2021, 11:46 PMrook
11/17/2021, 3:51 PMsealed class Result<T>(
val data: T?
val exception: Exception?
) {
class Success<T>(data: T): Result<T>(data, null)
class Error(exception: Exception): Result<Nothing>(null, exception)
}
Given Success(14).toString()
, we can expect an output of Success[data=14]
.
Given Success("foo").toString()
, we can expect an output of Success[data=foo]
.
Given Error(Exception("foo"))
, we can expect an output of Error[exception=java.lang.Exception: foo]
The $varName
syntax is called string interpolation. it takes the variable supplied, calls the toString
function on it, and inserts that string into the string literal that contains it.
Hope that helps @Mehdi HaghgooMehdi Haghgoo
11/21/2021, 1:41 PM