mardi 29 juin 2021

Parsing strings to arbitrary enums in Kotlin Multiplatform

I was wondering whether it's possible to implement a function such as the following in Kotlin Multiplatform:

inline fun <reified T : Enum<T>> parseToEnum(value: String): T {
    // ?
}

Basically, a function that takes the type of an Enum and a string and then returns the enum value of enum T that matches the passed string (value).

I know that it is possible in Java (and therefore in Kotlin/JVM) but I fear that there might not be a way to do this in common multiplatform code, because there's very little reflection data retained at runtime.

If there's a way to do this (or something similar in functionality), please let me know.

Using Kotlin 1.5.20 (or generally the newest version, I can update if necessary)





Aucun commentaire:

Enregistrer un commentaire