'@StringRes annotation fails for extension function return type

I have an extension function for an enum that return a string resource. In order to make sure that the function only returns a valid String resource, I have annotated the function with @StringRes.

@StringRes
fun Fruit.getDescription(): Int {
    return when (this) {
        Fruit.APPLE -> R.string.apple_description
        Fruit.ORANGE -> R.string.orange_description
    }
}

However, even if I return a regular integer, a drawable or color resource etc, Android Studio doesn't seem to be flagging this as an issue. Which defeats the purpose of @StringRes annotation.

@StringRes
fun Fruit.getDescription(): Int {
    return when (this) {
        Fruit.APPLE -> 123
        Fruit.ORANGE -> R.drawable.some_icon
    }
}

How can I enforce the return type of my extension function to be a valid String resource ?

The annotation works for normal functions.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source