is there a way to create a generic function/method...
# getting-started
b
is there a way to create a generic function/method of instantiating an uppercase enum from a lowercase string? Been trying to get this to work to no avail:
Copy code
fun String.toEnumConstant(): String =
    this.split("(?=\\p{Upper})".toRegex())
        .joinToString("_") { it.uppercase() }


@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T : Enum<T>> Enum.Companion.fromLowercaseString(value: String): T? =
    enumEntries<T>().find { it.name == value.toEnumConstant() }

enum class Values {
    TEST
}

println(Values.fromLowercaseString("test"))
1
y
Copy code
import kotlin.enums.*
fun String.toEnumConstant(): String =
    this.split("(?=\\p{Upper})".toRegex())
        .joinToString("_") { it.uppercase() }


@OptIn(ExperimentalStdlibApi::class)
inline fun <reified T : Enum<T>> fromLowercaseString(value: String): T? =
    enumEntries<T>().find { it.name == value.toEnumConstant() }

enum class Values {
    TEST
}

fun main() {
	println(fromLowercaseString<Values>("test"))
}
Not sure if there's a simple way to have it work for every companion of every enum though
b
there seems to be special compiler magic at work
just as an aside, is there a way to call a reified function with a class instead of a type?
I think I could make my use case work with that
so basically: enumEntries(Values::class)
e
that wouldn't be possible to inline at compile time, so no