Daniele Segato
09/04/2018, 8:38 AM@IntDef(
flag = false,
value = {
AspectRatioMode.AUTO,
AspectRatioMode.BY_WIDTH,
AspectRatioMode.BY_HEIGHT,
}
)
public @interface AspectRatioMode {
int AUTO = 0;
int BY_WIDTH = 1;
int BY_HEIGHT = 2;
}
@IntDef
is an Android annotation allowing to define an annotation for Integer constants on Integers:
@AspectRatioMode
int myMode = AspectRatioMode.AUTO; // can only be one of AUTO / BY_WIDTH / BY_HEIGHT
While in kotlin the conversion would be:
@IntDef(flag = false, value =[
AspectRatioMode.AUTO,
AspectRatioMode.BY_WIDTH,
AspectRatioMode.BY_HEIGHT
])
annotation class AspectRatioMode {
companion object {
val AUTO = 0
val BY_WIDTH = 1
val BY_HEIGHT = 2
}
}
but I get an error on `companion object`: "Members are not allowed in annotation class"
I can move those constants out:
object AspectRatioModeConst {
const val AUTO = 0
const val BY_WIDTH = 1
const val BY_HEIGHT = 2
}
@IntDef(flag = false, value = [
AspectRatioModeConst.AUTO,
AspectRatioModeConst.BY_WIDTH,
AspectRatioModeConst.BY_HEIGHT
])
annotation class AspectRatioMode
but that is a lot uglier then Java where I could more cleanly use the annotation directly for constants.
@AspectRatioMode
val myGoodMode : Int = AspectRatioMode.AUTO; // I want this
val myBadMode : Int = AspectRatioModeConst.AUTO; // not this
Is there any other way I can obtain the same result of Java with Kotlin?cbruegg
09/04/2018, 8:47 AMDaniele Segato
09/04/2018, 8:49 AMcbruegg
09/04/2018, 8:50 AMcbruegg
09/04/2018, 8:51 AMDaniele Segato
09/04/2018, 8:59 AMDaniele Segato
09/04/2018, 9:00 AMDaniele Segato
09/04/2018, 9:00 AMDaniele Segato
09/04/2018, 9:01 AMgildor
09/04/2018, 9:45 AMgildor
09/04/2018, 9:46 AM