0

As the title suggests, I want to define multiple levels of @IntDef. In java for example, one can do -

@IntDef({OPTIONS.OPTIONS_A.AA,
        OPTIONS.OPTIONS_A.AB,
        OPTIONS.OPTIONS_A.OPTIONS_AA.AAA,
        OPTIONS.OPTIONS_A.OPTIONS_AA.AAB,
        OPTIONS.OPTIONS_B.BA})

public @interface OPTIONS {
    public @interface OPTIONS_A {
        int AA = 0;
        int AB = 1;

        public @interface OPTIONS_AA {
            int AAA = 10;
            int AAB = 11;
        }
    }

    public @interface OPTIONS_B {
        int BA = 2;
    }
}

How can I do that in Kotlin?

nirkov
  • 697
  • 10
  • 25
  • Why don't you use sealed classes instead? – Hamza Sharaf Aug 26 '21 at 22:13
  • I thought that will be better to using IntDef because it forces exclusive values, but you are right and this is a possible solution. – nirkov Aug 27 '21 at 07:35
  • 1
    You can define it in Kotlin using `object` for the inner classes instead of annotation classes, and make all the constants `const val` and put them in companion objects. But it won't matter, because [Kotlin will not enforce the use of the annotation](https://stackoverflow.com/questions/37833395/kotlin-annotation-intdef). With ART + R8 minification, IntDef is kind of obsolete now. You can use enum classes instead. – Tenfour04 Aug 27 '21 at 12:53
  • IntDef forces exclusive values which is good for my uses. There is some way to force that in Enums/static classes/Kotlin objects? – nirkov Aug 27 '21 at 15:50

0 Answers0