Search code examples
androidkotlinannotationskotlin-android-extensions

Kotlin - Set @Annotations . to function return types?


Is there a way to use annotations to declare a return condition for kotlin functions?

For example, I want to tell lint that my function will return an Android resource id at:

fun getImageId(): Int

I would want something as:

fun getImageId(): @DrawableRes Int

which fails

I thought it would make sense (I believe is possible in Java), because I may have something as:

 fun setImage(@DrawableRes res: Int) {
        myImageView.setImageResource(res)
    }

and call it as:

setImage(getImageId())

So that it has a chain of validations that the given int is actually a Res Id


Solution

  • You can do it with:

    @DrawableRes
    fun getImageId(): Int