AttributeData
AttributeData<T>
is an abstract sealed class that represents the value of an attribute.
The class hierarchy defines the status, and has this scheme:
Loaded
: represents and contains a value whose status is loaded.NotLoaded
: represents a value that is not yet loaded. It is a sealed class as well, that divides in:
To create AttributeData
s we can either use their constructor or call this convenience methods:
AttributeData.errorInstance("Task failed successfuly", "A descriptive description") //Creates an ERROR AttributeData
AttributeData.loadedInstance(1234) //Creates a LOADED AttributeData with the value 1234
AttributeData.loadingInstance() //Creates a LOADING AttributeData
AttributeData.errorInstance("Task failed successfuly", "A descriptive description"); //Creates an ERROR AttributeData
AttributeData.loadedInstance(1234); //Creates a LOADED AttributeData with the value 1234
AttributeData.loadingInstance(); //Creates a LOADING AttributeData
The nice things about this class is that is also extendable: for instance, imagine we want to create an attribute that downloads something from some APIs, but we want to also retain the information of what kind of error happened. We can easily extend the Error
class to accommodate our needs:
enum class ApiErrorType {
API_LIMIT_REACHED, SERVER_BUSY, BAD_REQUEST, SSL_ERROR, OTHER
}
class ApiError(val type: ApiErrorType) : Error("Error calling APIs: $type")
enum ApiErrorType {
API_LIMIT_REACHED, SERVER_BUSY, BAD_REQUEST, SSL_ERROR, OTHER
}
class ApiError extends Error<Nothing> {
public final ApiErrorType type;
public ApiError(ApiErrorType type) {
super("Error calling APIs: " + type);
this.type = type;
}
}
Now we can return this an instance of this error from our task and check for it when needed.