简体   繁体   English

Kotlin 中的 Enum.valueOf

[英]Enum.valueOf in Kotlin

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? inline fun <reified T : kotlin.Enum<T>> safeValueOf(type: kotlin.String?): T? is not satisfied: inferred type TestEnum?不满意:推断类型TestEnum? is not a subtype of kotlin.Enum<TestEnum?>不是kotlin.Enum<TestEnum?>的子类型

enum class TestEnum

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

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

Crash-safe Solution碰撞安全解决方案

Create an extension and then call valueOf<MyEnum>("value") .创建一个扩展,然后调用valueOf<MyEnum>("value") If the type is invalid, you'll get null and have to handle it如果类型无效,您将获得 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.或者,您可以设置默认值,调用valueOf<MyEnum>("value", MyEnum.FALLBACK)并避免空响应。 You can extend your specific enum to have the default be automatic您可以扩展您的特定枚举以将默认值设为自动

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

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

I would typically add something like this to enum:我通常会在枚举中添加这样的东西:

companion object {
    fun from(type: String?): TestEnum = values().find { it.name == type } ?: DEFAULT
}

this would make a clean call这将是一个干净的电话

val value = TestEnum.from("test")

of course you can make it return null当然你可以让它返回null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM