Add an Android annotation to a param of List<T> for type-hinting

611 Views Asked by At

Normally, if we have a int parameter id to present a resource ID in Android. we could add annotations like @StringRes or @DrawableRes from androidx.annotation. So that IDE could use the information to check type for us, which is nice.

Now, I have a method that signature looks like the snippet below. The type of the param ids is List<Int>. The element in the list should be one of the drawable ids, for example, R.drawable.background. But I don't know how to let IDE remind me or people who use this function shortly.

/**
 * @param ids the collection of drawable resources as candidates to be selected.
 */
fun ImageSelector(initialIndex: Int, ids: List<Int>, onSelected: (Int) -> Unit) {}

I have tried ids: List<@DrawableRes Int>, which did not working.

I write doc comments for the method for now. I am still wondering the way to achieve this, Although annotation is just a nice-to-have feature.

1

There are 1 best solutions below

0
Stuart Gilbert On

The solution I have in place at the moment is a wrapping data class like this:

data class StringResource(@StringRes val id: Int)

I use that in my parameterized types in place of an Int, and everything seems happy. I get a warning if I try to do something like StringResource(123456) instead of StringResource(R.string.blah_blah).