java Kotlin 中的 Enum.valueOf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35666815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:26:02  来源:igfitidea点击:

Enum.valueOf in Kotlin

javaenumskotlin

提问by AndroidEx

Is there a way to make something like this work in Kotlin without the reflection?

有没有办法在没有反射的情况下在 Kotlin 中做这样的事情?

inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: String?): T? {
    return java.lang.Enum.valueOf(T::class.java, type)
}

The example below doesn't compile due to:

以下示例无法编译,原因如下:

Type parameter bound for T in inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: kotlin.String?): T?is not satisfied: inferred type TestEnum?is not a subtype of kotlin.Enum<TestEnum?>

inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: kotlin.String?): T?不满足为 T in 绑定的类型参数:推断类型TestEnum?不是的子类型kotlin.Enum<TestEnum?>

enum class TestEnum

fun main() {
    val value: TestEnum? = safeValueOf("test")
}

采纳答案by yole

Your function works if you specify the type parameter value explicitly:

如果您明确指定类型参数值,您的函数将起作用:

val value = safeValueOf<TestEnum>("test")

The original code is supposed to work as well, but doesn't work because of a bug in the type inference implementation: https://youtrack.jetbrains.com/issue/KT-11218

原始代码应该也能工作,但由于类型推断实现中的错误而无法工作:https: //youtrack.jetbrains.com/issue/KT-11218

回答by Peter

Since Kotlin 1.1, it's possible to access the constants in an enum class in a generic way, using the enumValues() and enumValueOf() functions:

从 Kotlin 1.1 开始,可以使用 enumValues() 和 enumValueOf() 函数以通用方式访问枚举类中的常量:

enum class RGB { RED, GREEN, BLUE }

inline fun <reified T : Enum<T>> printAllValues() {
    print(enumValues<T>().joinToString { it.name })
}

printAllValues<RGB>() // prints RED, GREEN, BLUE

https://kotlinlang.org/docs/reference/enum-classes.html#working-with-enum-constants

https://kotlinlang.org/docs/reference/enum-classes.html#working-with-enum-constants

回答by Gibolt

Crash-safe Solution

碰撞安全解决方案

Create an extension and then call valueOf<MyEnum>("value"). If the type is invalid, you'll get null and have to handle it

创建一个分机,然后调用valueOf<MyEnum>("value"). 如果类型无效,您将获得 null 并且必须处理它

inline fun <reified T : Enum<T>> valueOf(type: String): T? {
    return try {
        java.lang.Enum.valueOf(T::class.java, type)
    } catch (e: IllegalArgumentException) {
        null
    }
}

Alternatively, you can set a default value, calling valueOf<MyEnum>("value", MyEnum.FALLBACK), and avoiding a null response. You can extend your specific enum to have the default be automatic

或者,您可以设置默认值,调用valueOf<MyEnum>("value", MyEnum.FALLBACK)并避免空响应。您可以扩展您的特定枚举以将默认值设为自动

inline fun <reified T : Enum<T>> valueOf(type: String, default: T): T {
    return try {
        java.lang.Enum.valueOf(T::class.java, type)
    } catch (e: IllegalArgumentException) {
        default
    }
}

Or if you want both, make the second:

或者,如果您想要两者,请制作第二个:

inline fun <reified T : Enum<T>> valueOf(type: String, default: T): T = valueOf<T>(type) ?: default