miqbaldc
05/06/2020, 5:54 PM// option 1
@SafeGetter // custom annotation
val list: MutableList<String> = emptyList()
// option 2 - custom class
class SafeMutableList<E> : MutableList<E> {
@SafeGetter
override fun get(index: Int) = super.get(index)
}
val list: SafeMutableList<String> = emptyList()
1. compile-time error
if (list.get(7)) { // when running/building this code, it will have a compile-time error
}
2. compile-time safe
list.add("default)
if (list.get(7)) { // this won't compile-time error
}
3. compile-time safe
val index = 7
if (0 <= index && index < list.size) {
if (list.get(index)) { // this won't compile-time error
}
}
the reason I wanna create @SafeGetter (a placeholder annotation name) is to prevent engineer forgot to handle IndexOutOfBoundsExceptions or NPE to reduce runtime error (in our apps)
does the above implementation possible? and if yes, is there any reference/tutorial I can read? or maybe slack channel discussion? (if #C0922A726 is not the right place to ask this kind of topic)