Is there a `@IntRange` annotation for kotlin which...
# android
l
Is there a
@IntRange
annotation for kotlin which I can use in kotlin only module? @IntRange works at compilation time right?
e
assuming you mean androidx.annotation.IntRange, that is only checked by Android Lint: https://developer.android.com/studio/write/annotations#value-constraint (so at build time, and only if Lint is run) you can write your own annotation for documentation purposes, there will just be nothing validating it
l
@ephemient What options do I have without lint? I could have a check at runtime. Any other options?
c
you can do a
Copy code
check(param in 1..3)
in your methods body at runtime
e
if it's for input validation,
require
(
IllegalArgumentException
) is better than
check
(
IllegalStateException
)
but it depends on what you want to do. runtime validation can be inserted by hand, of course, but that's not what Android's
@IntRange
does. if you want compile-time validation, you can either apply Android Lint to a non-Android project (this is supported) or build your own, possibly on #arrow-meta as it has some support for enabling this kind of data flow analysis
👍 1
l
ok thanks guys, this is a good overview. I guess I will keep it simple and require it at runtime. @ephemient It's a bit offtopic question but when I'm looking for compile time validation is Android Lint my only option?
e
Android Lint doesn't actually do a great job of data flow analysis (has lots of false negatives), and you could build your own on top of Arrow Analysis, but either way you would want something like that for compile-time validation
l
Yeah I have reported a few bugs in the last few weeks for Android Lint. Thanks for your opinion 🙂
623 Views