I have an enum:
public enum AppEnums { SERVICE_ERROR, CONNECTION_ERROR; }
and I want to use it in an intDef of Android Annotation:
@IntDef({AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR}) public @interface ServiceErrors { }
error shows:
incompatible types found, required: 'long'
What I can do with this incompatibility?
I don't want to handle values of AppEnum parameters manually, Enum create values automatically ordinarily. AppEnums.CONNECTION_ERROR.ordinal()
return int value of enum parameter but don't work here.
Kotlin |Java. @Retention(value = AnnotationRetention.SOURCE) @Target(allowedTargets = [AnnotationTarget.ANNOTATION_CLASS]) annotation IntDef. Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.
Enums often require more than twice as much memory as static constants. You should strictly avoid using enums on Android.
Enums could be treated as a special type of classes and annotations as a special type of interfaces. The idea of enums is simple, but quite handy: it represents a fixed, constant set of values. What it means in practice is that enums are often used to design the concepts which have a constant set of possible states.
Enum in java is a data type that contains fixed set of constants. When we required predefined set of values which represents some kind of data, we use ENUM. We always use Enums when a variable can only take one out of a small set of possible values.
You should strictly avoid using enums on Android." IntDef is a way of replacing an integer enum where there's a parameter that should only accept explicit int values. For example, suppose we want to record the type of a feed item as shown below:
You should strictly avoid using enums on Android." IntDef is a way of replacing an integer enum where there's a parameter that should only accept explicit int values. For example, suppose we want to record the type of a feed item as shown below: Right now there is no validations to ensure that the type passed into the constructor is valid.
Refer to the official Android annotations guide for more details. StringDef is similarly a way of replacing an string enum where there's a parameter that should only accept explicit string values. For example, suppose we want to record the value for a setting as shown below:
This annotation can then decorate a return value or a method parameter, giving hints to tools you use about the values that are accepted or can be expected. Their main purpose is to enforce type safety for method parameters and/or method return values while writing the source code.
The main idea of IntDef
annotation is to use set of int
constants like an enum
, but without enum
. In this case you have to declare all constants manually.
@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED}) @Retention(RetentionPolicy.SOURCE) @interface Status { int IDLE = 0; int PROCESSING = 1; int DONE = 2; int CANCELLED = 3; }
You can see detailed example here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With