I want to use Pair<Int, Int> as return type where one of the Int is annotated with @StringRes.

Pair<@param:StringRes Int, Int>给出了弃用警告.

推荐答案

你不能.问题是Android的@StringRes不适用于目标TYPE_USE(或者在Kotlin是TYPE).

It would work if it was defined like this (java):

@Retention(SOURCE)
@Target({METHOD, PARAMETER, FIELD, TYPE_USE})
public @interface StringRes {
}

Now it would work:

fun fooIt(p: Pair<@StringRes Int, Foo>) {}

还有一个悬而未决的问题:https://issuetracker.google.com/issues/109714923

但是,尽管出现了弃用警告,您的解决方案仍有可能真正起作用.我还没有测试过.

Kotlin相关问答推荐

UByte范围. Min_UTE.. UByte.MAX_UTE不符合预期

Kotlin—从列表中枚举属性计算不同值的数量

如何在Kotlin中模拟www.example.com()?

"Kotlin中的表达式

在Kotlin中的嵌套when语句中,else块是强制性的吗?

两个LocalDateTime之间的Kotlin差异

为什么会出现Kotlin.Unit错误以及如何修复它?

按钮无法在 Android Studio 上打开新活动

Jetpack Compose 中的连续重组

is return inside function definition 也是 kotlin 中的表达式

是否可以通过超时暂停协程?

runBlocking 中的 deferred.await() 抛出的异常即使在被捕获后也被视为未处理

什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

kotlin 扩展属性的惰性初始化器中的这个引用

DatabaseManager_Impl 不是抽象的,不会覆盖 RoomDatabase 中的抽象方法 clearAllTables()

Kotlin默认使用哪种排序?

Kotlin 的 Double.toInt() 中使用了哪种方法,舍入还是截断?

Kotlin替换字符串中的多个单词

Android studio 4.0 新更新版本说 Kotlin 与这个新版本不兼容

目前不支持 Gradle 项目的自动库版本更新.请手动更新您的 build.gradle